C Program to check whether two strings are anagrams of each other.

C Strings:
Write a c program to check whether two strings are anagrams of each other.
Two strings are said to be anagrams, if the characters in the strings are same in terms of numbers and value ,only arrangement or order of characters are may be different.
Example: “dfghjkl” and “lkjghdf” are anagrams of each other.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
# define NO_OF_CHARS 256
int Anagram(char *str1, char *str2)
{
    // Create two count arrays and initialize all values as 0
    int count1[NO_OF_CHARS] = {0};
    int count2[NO_OF_CHARS] = {0};
    int i;
    
    // For each character in input strings, increment count in
    // the corresponding count array
    for (i = 0; str1[i] && str2[i];  i++)
    {
        count1[str1[i]]++;
        count2[str2[i]]++;
    }
    
    // If both strings are of different length. Removing this condition
    // will make the program fail for strings like "aaca" and "aca"
    if (str1[i] || str2[i])
    return 0;
    
    // Compare count arrays
    for (i = 0; i < NO_OF_CHARS; i++)
    if (count1[i] != count2[i])
    return 0;

    return 1;
}
main()
{
    char str[100], str1[100];
    int flag = 0;
    
    printf("Enter first stringn");
    gets(str);
    
    printf("Enter second stringn");
    gets(str1);
    
    flag=Anagram(str, str1);
    if (flag==1)
    printf(""%s" and "%s" are anagrams.n", str, str1);
    else
    printf(""%s" and "%s" are not anagrams.n", str, str1);
    
    return 0;
}

Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

2 comments on “C Program to check whether two strings are anagrams of each other.

  • Jerica Tunney says:

    I just install WordPress. I post several posts but they all proceed to homepage. How can I let posts move to different web page, for example , I have ‘articles’ tabs and ”events’ tab. I would like articles listings go to articles section and I would like events listings go to events section..

    Reply
    • Sandeepa Nadahalli says:

      Hi Jerica,
      Please share your email address or Skype id. I will contact you and help you with your problem.

      Reply

Leave a Reply