A string is called prime if it can't be constructed by concatenating multiple equal strings, implement a program which outputs whether given string is prime or not
Answers
A program that outputs whether a given string is prime or not is as follows:
import java.util.Scanner;
public class Pro
{
public static void main(String[] args) {
Scanner sci = new Scanner(System.in);
String lines = sci.nextLine();
if(isPrime(lines))
System.out.println("prime! it is");
else
System.out.println("not prime! it is");
}
public static boolean isPrime(String lines){
String substr;
for(int i=0; i<lines.length()/2; i++){
substr = line.substring(0, i+1);
if(occurrenceCount(lines, substr) *
substr.length() == lines.length()){
return false;
}
}
return true;
}
public static int occurrenceCount(String aps,
String substr)
{
String boe = new String(aps);
return (boe.length() - b.replace(substr,
"").length()) / substr.length();
}
}
- The above program is written in java programming language.