Case Insensitive Anagram Check
This program checks anagrams ignoring case.
Problem Statement
Write a Java program to check if two strings are anagrams ignoring case.
Source Code
| 1 | import java.util.Arrays; |
| 2 | public class CaseInsensitiveAnagram { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Case Insensitive Anagram"); |
| 5 | String s1="Listen", s2="Silent"; |
| 6 | char[] a1=s1.toLowerCase().toCharArray(); |
| 7 | char[] a2=s2.toLowerCase().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 Case Insensitive Anagram Strings are anagrams
Explanation
Both strings are converted to lowercase before being turned into sorted character arrays, so case differences do not affect the comparison, demonstrating a case-insensitive anagram check.