Check Anagram Strings
This program checks if two strings are anagrams.
Problem Statement
Write a Java program to check if two strings are anagrams.
Source Code
| 1 | import java.util.Arrays; |
| 2 | public class AnagramCheck { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Anagram Check"); |
| 5 | String s1="listen", s2="silent"; |
| 6 | char[] a1=s1.toCharArray(); |
| 7 | char[] a2=s2.toCharArray(); |
| 8 | Arrays.sort(a1); Arrays.sort(a2); |
| 9 | if(Arrays.equals(a1,a2)) System.out.println("Strings are anagrams"); |
| 10 | else System.out.println("Strings are not anagrams"); |
| 11 | } |
| 12 | } |
Program Output
Eduinq Anagram Check Strings are anagrams
Explanation
Both strings are converted into character arrays and sorted, and Arrays.equals() then checks whether the sorted arrays match exactly, demonstrating a classic anagram check.