How To Determine If A Word Is An Anagram

What is meant by anagram?

 
An anagram is a word or phrase made by using the letters of another word or phrase in a different order.
 
For example, "silent" is an anagram of "listen".
 
Given below is a simple C# program to determine if two words are anagrams.
  1. class Program {  
  2.     public class Anagram {  
  3.         public bool areAnagram(string firstString, string secondString) {  
  4.             if (firstString.Length != secondString.Length) {  
  5.                 return false;  
  6.             }  
  7.             //Convert string to character array  
  8.             char[] firstCharsArray = firstString.ToLower().ToCharArray();  
  9.             char[] secondCharsArray = secondString.ToLower().ToCharArray();  
  10.             //Sort array  
  11.             Array.Sort(firstCharsArray);  
  12.             Array.Sort(secondCharsArray);  
  13.             //Check each character and position.  
  14.             for (int i = 0; i < firstCharsArray.Length; i++) {  
  15.                 if (firstCharsArray[i].ToString() != secondCharsArray[i].ToString()) {  
  16.                     return false;  
  17.                 }  
  18.             }  
  19.             return true;  
  20.         }  
  21.     }  
  22.     static void Main(string[] args) {  
  23.         string firstString, secondString;  
  24.         //Gets words from user.  
  25.         Console.WriteLine("Enter first string");  
  26.         firstString = Console.ReadLine();  
  27.         Console.WriteLine("Enter second string");  
  28.         secondString = Console.ReadLine();  
  29.         Anagram anagram = new Anagram();  
  30.         //Check if words are anagram  
  31.         if (anagram.areAnagram(firstString, secondString) == true) {  
  32.             Console.WriteLine("Both string are anagram string.");  
  33.         } else {  
  34.             Console.WriteLine("Both string are not anagram string.");  
  35.         }  
  36.         Console.ReadLine();  
  37.     }  
  38. }  

About the code

 
The basic idea is to get two words from the user and determine if these are anagram strings or not.
 
In the above program, first, we get two words from the user and then, check if both the strings are of same length. If their length is different, then these words are not anagrams.
 
If both strings are of the same length, the next step is to split the word into a character array and convert them into either lower or upper case to enable case insensitivity. After that, we sort this character array in ascending/descending order and check if each character and its position is the same in both the arrays or not.
 

For example

 
If a user enters "listen" and "silent", then first, both of these will be converted into two character arrays respectively. After that, the arrays are sorted in ascending/descending order. Then finally, we check each of the character and their position. Since it is the same, these strings are anagram strings.
Next Recommended Reading Remove AM PM from Time String using C#