String Anagram in C

Last Updated : 23 Jul, 2025

An anagram of a string is another string can be formed by the rearrangement of the same set of characters. In this article, we will learn how to check whether two strings are anagrams of each other in the C.

The simplest method to check whether two strings are anagrams of each other is by counting frequency of each character in both the strings and checking whether all characters have same count or not. Let’s take a look at an example:

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

int anagram(char *s1, char *s2) {

    // Count arrays
    int c1[256] = {0};
    int c2[256] = {0};

    // Count frequency of s1's characters
  	int i = 0;
    while(s1[i]) c1[s1[i++]]++;
    
    // Count frequency of s2's characters
  	i = 0;
    while(s2[i]) c2[s2[i++]]++;


    // If any character count mismatch
    // strings are not anagrams
    for (int i = 0; i < 256; i++) {
        if (c1[i] != c2[i]) return 0;
    }

  	// Strings are anagram is all frequencies match
    return 1;
}

int main() {
    char s1[] = "listen";
    char s2[] = "silent";

  	// Checking if s1 and s2 are anagram
    if (anagram(s1, s2))
        printf("Anagrams");
    else
        printf("Not Anagrams");

    return 0;
}

Output
Anagrams

Note: If the input strings contain a larger set of characters, such as Unicode characters, using an array for frequency count may not be feasible.

There are also a few other methods in C to check whether two strings are anagrams of each other. Some of them are as follows:

Using Sorting

Sorting two anagram strings using qsort() will arrange their characters in the same order making both strings identical. The strcmp() function can be then used to check whether the two strings are identical or not.

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

// Comparator for qsort()
int comp(const void* a, const void* b) {
  	return strcmp((char*)a, (char*)b);
}

int anagram(char *s1, char *s2) {
  
    // If lengths are not the same, they cannot be anagrams
    if (strlen(s1) != strlen(s2))
        return 0;

    // Sort both strings
    qsort(s1, strlen(s1), sizeof(char), comp);
    qsort(s2, strlen(s2), sizeof(char), comp);

    // If the sorted strings are the same, they are anagrams
    return strcmp(s1, s2) == 0;
}

int main() {
    char s1[] = "listen";
    char s2[] = "geeks";

    // Checking if s1 and s2 are anagram
    if (anagram(s1, s2))
        printf("Anagrams");
    else
        printf("Not Anagrams");
  
    return 0;
}

Output
Not Anagrams

Using Optimized Frequency Counting

The frequency counting method can be optimized to use a single frequency array instead of two by counting the frequencies of both strings within the same array. Also, if the length of the strings is not same, they cannot be anagram.

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

int anagram(char *s1, char *s2) {
  
    // If lengths are not same, they cannot be anagrams
    if (strlen(s1) != strlen(s2))
        return 0;

    // Count array
    int c[256] = {0};

    // Add characters of s1 to count and remove characters of s2 from count
  	int i = 0;
    while(s1[i]) {
        c[s1[i]]++;
        c[s2[i]]--;
        i++;
    }

    // If any character have a count left
  	// then strings are not anagram
    for (int i = 0; i < 256; i++) {
        if (c[i] != 0) return 0;
    }

  	// Strings are anagram is all frequencies are 0
    return 1;
}

int main() {
    char s1[] = "listen";
    char s2[] = "silent";

  	// Checking if s1 and s2 are anagram
    if (anagram(s1, s2))
        printf("Anagrams");
    else
        printf("Not Anagrams");

    return 0;
}

Output
Anagrams
Comment