C Program to check strings are anagram or not


In this program, You will learn how to check strings are anagram or not in c.


Enter first string: triangle

Enter second string: integral

If Both string characters are same in any order it's called anagram


Example: How to check strings are anagram or not in c

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

    char str1[100];
    char str2[200];
    int len1, len2, c = 0;
    int j, i;

    printf("Enter First String :");
    fgets(str1, 100, stdin);
    strtok(str1, "\n");

    printf("\nEnter Second String :");
    fgets(str2, 100, stdin);
    strtok(str2, "\n");

    len1 = strlen(str1);
    len2 = strlen(str2);

    if (len1 == len2) {
        for (i = 0; i < len1; i++) {
            j = 0;
            while (str2[j] != '\0') {
                if (str1[i] == str2[j]) {
                    c++;
                    break;
                }
                j++;
            }
        }
    }

    if (len1 == c) {
        printf("\nStrings are Anagram");
    } else {
        printf("\nStrings are Not Anagram");
    }

    return 0;
}

Output:

Enter First String :triangle
Enter Second String :integral
Strings are Anagram