Apr 17, 2012

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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*                      Happy Coding

# 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++)
    // 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;
    char str[100], str1[100];
    int flag = 0;
    printf("Enter first string\n");
    printf("Enter second string\n");
    flag=Anagram(str, str1);
    if (flag==1)
    printf("\"%s\" and \"%s\" are anagrams.\n", str, str1);
    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

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