Updated July 4, 2023
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 strings must be the same.
Two strings, “abc” and “cab,” have the same frequency of the letters a, b, and c. By rearranging the letters in “abc,” we can form the string “cab.” Thus, “abc” and “cab” are anagrams. The program calculates the frequency of each letter when the user inputs two strings. 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: To proceed, you need to define two strings.
- 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 another string.
- Step 3: You should change the characters of both strings to lowercase if they have the same length. We do this conversion to make a comparison between the two strings easier.
- Step 4: The next step is to sort the characters in the strings. In some 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 a character array must be sorted.
- Step 6: Finally, the content is checked for equality.
Examples of Anagram Programs in C
Given below are the examples of Anagram Programs in C:
Example #1
C Program to find if the given two strings are anagrams or not using the sorting method.
We first compare the lengths of both strings, then sort and compare them.
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.
The program calculates the lengths of strings and compares them using 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 by calculating the characters’ frequency.
This program will calculate the frequency of each character in two strings and compare them to each other.
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 are 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 anagrams or not. Then we understand different C program types to check whether the given strings are anagrams and their output snapshots after execution.
Recommended Articles
This is a guide to Anagram Program in C. Here we discuss the introduction, algorithm, and examples of the Anagram Program in C. You may also have a look at the following articles to learn more –