-
Notifications
You must be signed in to change notification settings - Fork 87
/
checkstringsanagram.java
44 lines (35 loc) · 1.11 KB
/
checkstringsanagram.java
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
36
37
38
39
40
41
42
43
44
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// take input from users
System.out.print("Enter first String: ");
String str1 = input.nextLine();
System.out.print("Enter second String: ");
String str2 = input.nextLine();
// check if length is same
if(str1.length() == str2.length()) {
// convert strings to char array
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
// sort the char array
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// if sorted char arrays are same
// then the string is anagram
boolean result = Arrays.equals(charArray1, charArray2);
if(result) {
System.out.println(str1 + " and " + str2 + " are anagram.");
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
input.close();
}
}