How to check Two string are Anagrams in C Language?

The Test Coder
3 min readFeb 6

--

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. Here, we’ll discuss how to check if two strings are anagrams in the C programming language.

Anagrams have been a part of human history for centuries. Learn how to write a program to check if two words are anagrams using C language and discover interesting facts about this fascinating world of word play. This guide covers everything from the basics of anagrams to advanced examples and practical applications. Start exploring the exciting world of anagrams today!

Here’s an example of a code that checks if two strings are anagrams:

#include <stdio.h>
#include <string.h>

int areAnagram(char *str1, char *str2)
{
int n1 = strlen(str1);
int n2 = strlen(str2);
if (n1 != n2)
return 0;
int count[26] = {0};
for (int i = 0; i < n1; i++)
{
count[str1[i]-'a']++;
count[str2[i]-'a']--;
}
for (int i = 0; i < 26; i++)
if (count[i])
return 0;
return 1;
}

int main()
{
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
if (areAnagram(str1, str2))
printf("The two strings are anagrams.");
else
printf("The two strings are not anagrams.");
return 0;
}

This code first reads two strings from the user and then passes them to the areAnagram function. The areAnagram function first checks if the lengths of the two strings are equal. If they are not equal, then the two strings cannot be anagrams, and the function returns 0. If the lengths are equal, then the function initializes an array count to keep track of the frequency of each character in the two strings. The for loop iterates through each character of the two strings and increments the count of the corresponding character in str1 and decrements the count of the corresponding character in str2. After the loop, the function iterates through the count array and checks if all the elements are 0. If all the elements are 0, then the two strings are anagrams and the function returns 1. If any of the elements are not 0, then the two strings are not anagrams and the function returns 0.

In the main function, the value returned by the areAnagram function is checked, and a corresponding message is printed.

In this example, we’ve used the scanf function to read the strings from the user. If you want to read the strings including spaces, you can use the gets function or the fgets function. The gets function is not recommended for use as it is vulnerable to buffer overflow attacks.

Interesting Facts About Anagrams

Anagrams have been used for various purposes throughout history, including cryptography, word play, and code breaking. Here are a few more interesting facts about anagrams:

  1. Anagrams have been used for centuries as a form of word play and puzzle. Some famous examples of anagrams include “Debit card” to “Bad credit,” “Listen” to “Silent,” and “Elvis” to “Lives.”
  2. Anagrams have also been used in cryptography and code breaking. For example, during World War II, the Germans used anagrams as part of their encryption methods.
  3. Anagrams have been used in literature, poetry, and other forms of art. Poets and writers have used anagrams to create word puzzles and add an extra layer of meaning to their work.
  4. Anagrams can be used to form new words. For example, the word “anagram” itself is an anagram of “nag a ram.”
  5. Anagrams can also be used to create mnemonics, or memory aids. Mnemonics use anagrams to help people remember complex or difficult information by creating a memorable phrase or sentence.

In conclusion, anagrams have been used for a variety of purposes, including word play, cryptography, literature, and memory aids. These simple word puzzles can provide hours of entertainment and challenge for people of all ages and can also have practical applications.

In this article, we discussed how to check if two strings are anagrams in C. This simple example demonstrates the basic approach to checking anagrams in C. You can extend this example to handle other types of inputs and edge cases as per your requirements.

--

--