Introduction to Anagram Program in C
Two strings are said to be anagrams of each other if one string can be converted to form another string by rearranging the letters of one string and the number of characters in both the strings must be the same.
Consider two strings “abc” and “cab” for example, the occurrence of the characters a, b and c are same in both the strings and when all the characters in the string “abc” are rearranged, the string “cab” can be formed, hence the strings “abc” and “cab” are anagrams. Whenever two strings are given as input by the user, the frequency of each letter is calculated. The frequency of a letter is the number of occurrences of the letter in the string.
Algorithm
Algorithm to find whether the given two Strings are Anagram or Not?
- Step 1: Two strings must be defined.
- Step 2: Find out the length of each string. The strings are not anagrams if the length of one string is not equal to the length of other string.
- Step 3: If the lengths of the two strings are equal, the characters in the string must be converted to lower case letters. We do this conversion in order to make a comparison between the two strings easier.
- Step 4: The next step is to sort the characters in the strings. In some of the programming languages, inbuilt functions are available for sorting of strings. If there are no inbuilt functions to sort the strings, convert the strings to a character array.
- Step 5: The strings converted to character array must be sorted.
- Step 6: Finally, the content is checked for equality.
Examples of Anagram Program in C
Given below are the examples of Anagram Program in C:
Example #1
C Program to find if the given two strings are anagrams or not using the sorting method.
In this program, both the strings are sorted and then compared, after comparing the lengths of the strings.
Code:
#include <stdio.h>
#include <string.h>
//Declare the function names that are defined in the program later
void converttoLowercase(char[]);
void Arraysort(char[]);
int main ()
{
char string1[] = "Rat", string2[] = "Tar";
int a1, b = 0;
//length of strings is compared
if(strlen(string1) != strlen(string2))
{
printf("Both the strings are not anagram");
return 0;
}
else
{
//the strings are converted to lowercase
converttoLowercase(string1);
converttoLowercase(string2);
//The arrays are sorted by calling the function Arraysort()
Arraysort(string1);
Arraysort(string2);
for(a1 = 0; a1 < strlen(string1); a1++)
{
if(string1[a1] != string2[a1])
{
printf("Both the strings are not anagram");
return 0;
}
}
printf("Both the strings are anagram");
}
return 0;
}
void converttoLowercase(char a[])
{
int c;
for(c = 0; c < strlen(a)-1; c++)
{
a[c] = a[c]+32;
}
}
void Arraysort(char a[])
{
int temperory = 0,k,l;
for(k = 0; k < strlen(a)-1; k++)
{
for (l = k+1; l < strlen(a); l++)
{
if(a[k] > a[l])
{
temperory = a[k];
a[k] = a[l];
a[l] = temperory;
}
}
}
}
Output:
Example #2
C Program to find if the given two strings are anagrams or not using nested for loops.
In this program, the lengths of the strings are calculated and then compared inside nested for loops.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char string1[20], string2[20];
int leng, leng1, leng2, a, b, found1=0, not_found1=0;
printf("first string must be entered: ");
gets(string1);
printf("second string must be entered: ");
gets(string2);
//length of the first string is calculated
leng1 = strlen(string1);
//length of the first string is calculated
leng2 = strlen(string2);
//compare the length of the two strings to find out if the strings are anagram or not
if(leng1 == leng2)
{
leng = leng1;
for(a=0; a<leng; a++)
{
found1 = 0;
for(b=0; b<leng; b++)
{
if(string1[a] == string2[b])
{
found1 = 1;
break;
}
}
if(found1 == 0)
{
not_found1 = 1;
break;
}
}
if(not_found1 == 1)
printf("\nThe two entered strings are not Anagram");
else
printf("\nThe two entered strings are Anagram");
}
else
printf("\nsame number of characters must be present in both the strings to be an Anagram");
getch();
return 0;
}
Output:
Example #3
C Program to find if the given two strings are anagrams or not by calculating the frequency of characters.
In this program, the frequency of each character in each string is calculated and then compared with the frequency of the other string.
Code:
#include <stdio.h>
int anagram(char [], char []);
int main()
{
char i[100], j[100];
printf("two strings must be entered\n");
gets(i);
gets(j);
//checking anagrams
if (anagram(i, j) == 1)
printf("The given two strings are anagrams\n");
else
printf("The given two strings are not anagrams\n")
return 0;
}
int anagram(char i[], char j[])
{
int first1[26] = {0}, second1[26] = {0}, d=0;
// the frequency of characters in the first string is calculated
while (i[d] != '\0')
{
first1[i[d]-'a']++;
d++;
}
d = 0;
while (j[d] != '\0')
{
second1[j[d]-'a']++;
d++;
}
// the frequency of characters in the second string is calculated
for (d = 0; d < 26; d++)
{
if (first1[d] != second1[d])
return 0;
}
return 1;
}
Output:
Example #4
C Program to find if the given two strings are anagrams or not by converting to ASCII values of alphabets.
In this program, the ASCII values of each character in one string is found out and then compared with the ASCII values of the other string.
Code:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
# define NO_OF_CHARACTERS 26
// Checking if the given strings are anagrams using functions
bool Anagram(char *Test1, char *Test2)
{
// two count arrays are created and initialized to 0
int Count1[NO_OF_CHARACTERS] = {0};
int Count2[NO_OF_CHARACTERS] = {0};
int r=0;
if (strlen(Test1) != strlen(Test2))
return false;
// count is incremented in count array for each character in the given input strings
//the ascii value of 'a' is 97
for (r = 0; Test1[r] && Test2[r]; r++)
{
Count1[Test1[r]-97]++;
Count2[Test2[r]-97]++;
}
// count arrays are compared by using the assigned value to NO_OF_CHARACTERS
for (r = 0; r < NO_OF_CHARACTERS; r++)
if (Count1[r] != Count2[r])
return false;
return true;
}
int main()
{
char Test1[] = "grab";
char Test2[] = "brag";
if (Anagram(Test1, Test2))
printf("The two strings are anagram of each other");
else
printf("The two strings are not anagram of each other");
return 0;
}
Output:
Conclusion
In this tutorial, we understand the concept of anagrams through definitions and examples. And then understand the algorithm to check if the given two input strings are anagram or not. Then we understand different C program types to check if the given strings are anagram or not along with their output’s snapshots after execution.
Recommended Articles
This is a guide to Anagram Program in C. Here we discuss the introduction, algorithm, and examples of Anagram Program in C. You may also have a look at the following articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses