Let’s learn anagram java program.
anagram java program
Two strings can be an anagram string if characters of one string contain same characters of another string by regrouping the characters.
For example:
String1 = “silent”;
String2 = “listen”;
In the above example String1 and String2 are two different strings.
By regrouping the characters of String1 we have another string String2. Hence above two strings are anagram.
To understand anagram program first learn strings and arrays.
Now let’s see string anagram program.
import java.util.Arrays; public class Anagram { public static void main(String[] args) { String strOne = "silent"; String strTwo = "listen"; // checking if two strings length are same if(strOne.length() == strTwo.length()) { // converting strings to char array char[] charOne = strOne.toCharArray(); char[] charTwo = strTwo.toCharArray(); // sorting character array Arrays.sort(charOne); Arrays.sort(charTwo); // if sorted character arrays are same then the string is anagram boolean output = Arrays.equals(charOne, charTwo); if(output) { System.out.println(strOne + " and " + strTwo + " are anagram."); } else { System.out.println(strOne + " and " + strTwo + " are not anagram."); } } else { System.out.println(strOne + " and " + strTwo + " are not anagram."); } } }
Output:
silent and listen are anagram.
anagram program using scanner
Now let’s learn to print anagrams using scanner or anagram program using scanner.
import java.util.Arrays; import java.util.Scanner; public class PrintAnagrams { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter first string: "); String strOne = sc.nextLine(); System.out.print("Please enter second string: "); String strTwo = sc.nextLine(); // checking if length are same if(strOne.length() == strTwo.length()) { // converting strings to character array char[] charOne = strOne.toCharArray(); char[] charTwo = strTwo.toCharArray(); // sorting character array Arrays.sort(charOne); Arrays.sort(charTwo); // if sorted character arrays are same then the string is anagram boolean result = Arrays.equals(charOne, charTwo); if(result) { System.out.println(strOne + " and " + strTwo + " are anagram."); } else { System.out.println(strOne + " and " + strTwo + " are not anagram."); } } else { System.out.println(strOne + " and " + strTwo + " are not anagram."); } sc.close(); } }
Output:
Please enter first string: triangle
Please enter second string: integral
triangle and integral are anagram.
Please enter first string: arun
Please enter second string: kumar
arun and kumar are not anagram.
string anagram program using function
In the below program we are going to write a function to check whether two strings are anagram.
import java.util.Arrays; public class AnagramUsingFunction { public static void main(String[] args) { char[] ch1 = { 'e', 'l', 'v', 'i', 's' }; char[] ch2 = { 'l', 'i', 'v', 'e', 's' }; if(checkAnagram(ch1, ch2)) { System.out.println("two strings are anagram."); } else { System.out.println("two strings are not anagram."); } } static boolean checkAnagram(char[] ch1, char[] ch2) { int len1 = ch1.length; int len2 = ch2.length; // if length of both strings is not equal it cannot be anagram if(len1 != len2) return false; // sorting both strings Arrays.sort(ch1); Arrays.sort(ch2); // compare sorted strings for(int a = 0; a < len1; a++) if(ch1[a] != ch2[a]) return false; return true; } }
Output:
two strings are anagram.