-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramInString.java
More file actions
38 lines (38 loc) · 978 Bytes
/
Copy pathAnagramInString.java
File metadata and controls
38 lines (38 loc) · 978 Bytes
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
//WAP to check the both String is anagram or not.
import java.util.*;
public class AnagramInString{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first String : ");
String a = sc.nextLine();
System.out.println("Enter the Second String : ");
String b = sc.nextLine();
boolean anagram = anagramInString(a,b);
if(anagram==true)
{
System.out.println("String is Anagram");
}
else
System.out.println("String is not Anagram");
}
public static boolean anagramInString(String a, String b)
{
if(a == null || b==null || a.length()!=b.length())
{
return false;
}
a = a.toLowerCase();
b = b.toLowerCase();
char[] c = a.toCharArray();
char[] d = b.toCharArray();
Arrays.sort(c);
Arrays.sort(d);
String A = new String(c);
String B = new String(d);
if(A.equals(B))
{
return true;
}
return false;
}
}