Anagram Program in C | Anagram program in C Using String
What is Anagram?
The two strings are called as Anagram of each other if they contain the same characters either arranged in sequence or not it is not necessary.
How to check two Strings are Anagram or not?
To check whether the given two strings are Anagram of each other or not the compiler will ask the user to enter the two strings to check. After the input given by the user, the program will start executing are check whether the strings are Anagram or not.
After executing the compiler will display the output. The two strings are Anagram if and only if characters in that strings are the same sequence is not mandatory.
For Example abcd and dcba, creative and reactive, course and source are Anagram of each other.
Anagram Algorithm:
1 Step: Declare two Strings.
2 Step: Find out the length of two Strings (Strings are not anagram if the length of strings is not the same).
3 Step: Even if the lengths are equal the two strings should be in lowercase because it makes easy to check.
4 Step: Now sort the characters in the strings.For, sorting convert the strings into a character array.
5 Step: After converting character array must be sorted.
6 Step: In the last step, the Anagram is checked.
There are different ways to check the Anagram program in C we will see it one by one.
1. Anagram Program in C Using Sorting
In this program, we have declared two strings abcd and cabd. Now, the program will check whether the two Strings are Anagram or not.
Output:

2. Anagram program in C Using Nested For Loop
In this program, we will not declare strings instead we will ask the user to enter a first and second string. If the length of strings is not the same then the program will exit so length should be the same.
Output:


3. By Calculating Frequency Of Characters
In this program, the frequency of each character is calculated and this frequency is compared with the frequency of other string. If the frequency is same then the two strings are Anagram else Not Anagram.
Output:


4. By comparing ASCII values of two strings
In this program, two Strings course and source are declared, then the compiler compares the ASCII values of two strings. If ASCII values of two strings are same the Strings are Anagram else Not Anagram.
Output:

Post a Comment