-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_for_two_anagrams_string.java
More file actions
53 lines (42 loc) · 1.34 KB
/
check_for_two_anagrams_string.java
File metadata and controls
53 lines (42 loc) · 1.34 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//16.Program to Check whether two Strings are Anagrams
import java.util.*;
class check_for_two_anagrams_string
{
public static boolean anagram(char[] s1,char[] s2)
{
int l1=s1.length;
int l2=s2.length;
if(l1!=l2)
return false;
Arrays.sort(s1);
Arrays.sort(s2);
for(int i=0;i<l1;i++)
{
if(s1[i]!=s2[i])
return false;
}
return true;
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the 1st character array :");
char str1[]=sc.next().toCharArray(); //Input in a single line...
for (int i = 0; i < str1.length; i++)
System.out.print(str1[i]);
System.out.println("\n");
System.out.print("Enter the 2nd character array :");
char str2[]=sc.next().toCharArray(); //Input in a single line...
for (int i = 0; i < str2.length; i++)
System.out.print(str2[i]);
System.out.println("\n");
if(anagram(str1,str2))
{
System.out.println("The two strings are Anagram");
}
else
{
System.out.println("The two strings are NOT Anagram");
}
}
}