This Java code defines an Anagram class with a main method to check if two input strings are anagrams of each other. It converts the strings to lowercase, checks they are the same length, converts them to character arrays, sorts the arrays, and compares them to see if they are equal, printing the result.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views1 page
Dsa Assignment
This Java code defines an Anagram class with a main method to check if two input strings are anagrams of each other. It converts the strings to lowercase, checks they are the same length, converts them to character arrays, sorts the arrays, and compares them to see if they are equal, printing the result.
if (str1.length() != str2.length()) { System.out.println("Both the strings are not anagram"); } else { //Converting both the arrays to character array char[] string1 = str1.toCharArray(); char[] string2 = str2.toCharArray();
//Sorting the arrays using in-built function sort ()
Arrays.sort(string1); Arrays.sort(string2);
//Comparing both the arrays using in-built function equals ()
if(Arrays.equals(string1, string2) == true) { System.out.println("Both the strings are anagram"); } else { System.out.println("Both the strings are not anagram"); } } } }