-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeStrings.java
More file actions
35 lines (35 loc) · 1.02 KB
/
PrimeStrings.java
File metadata and controls
35 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*A String is called prime if it can't be constructed by concatenating multiple (more than one) equal strings.
"abac" is prime, but "xyxy" is not ("xyxy" = "xy" + "xy").
Implement a program which outputs whether a given string is prime.
Input: "xyxy"
Output: "not prime"
Input: "aaaa"
Output: "not prime"
Input: "hello"
Output: "prime"
*/
import java.util.*;
public class PrimeStrings{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the string to check if its Prime or not");
String str = in.nextLine();
char ch1[] = new char[str.length()/2+1];
char ch2[] = new char[str.length()/2+1];
int index1 = 0, index2 = 0;
for (int i=0;i<str.length() ;i++ ) {
if (i<str.length()/2) {
ch1[index1++] = str.charAt(i);
}else{
ch2[index2++] = str.charAt(i);
}
}
str = new String(ch1);
String str2 = new String(ch2);
if (str.equals(str2)) {
System.out.println("Not Prime");
}else{
System.out.println("Prime");
}
}
}