What is an anagram in C language?

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all original letters exactly once. In C programming, we check if two strings are anagrams by comparing the frequency of each character in both strings.

Two strings are anagrams if they contain the same characters with the same frequency, regardless of their order. For example, "listen" and "silent" are anagrams because both contain one 'l', one 'i', one 's', one 't', one 'e', and one 'n'.

Syntax

int check_anagram(char str1[], char str2[]);

Algorithm

The anagram checking algorithm follows these steps −

  • Create frequency arrays for both strings to count character occurrences
  • Count the frequency of each character (a-z) in both strings
  • Compare the frequency arrays − if identical, strings are anagrams

Example 1: Basic Anagram Check

This example demonstrates checking if two strings are anagrams using frequency counting −

Note: This program uses predefined strings to avoid input complications in online compilation.

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

int check_anagram(char a[], char b[]) {
    int first[26] = {0}, second[26] = {0}, c = 0;
    
    /* Check if lengths are different */
    if (strlen(a) != strlen(b))
        return 0;
    
    /* Calculate frequency of characters in first string */
    while (a[c] != '\0') {
        if (a[c] >= 'a' && a[c] <= 'z')
            first[a[c] - 'a']++;
        c++;
    }
    
    /* Calculate frequency of characters in second string */
    c = 0;
    while (b[c] != '\0') {
        if (b[c] >= 'a' && b[c] <= 'z')
            second[b[c] - 'a']++;
        c++;
    }
    
    /* Compare frequency arrays */
    for (c = 0; c < 26; c++) {
        if (first[c] != second[c])
            return 0;
    }
    return 1;
}

int main() {
    char str1[] = "listen";
    char str2[] = "silent";
    
    printf("String 1: %s
", str1); printf("String 2: %s
", str2); if (check_anagram(str1, str2)) printf("The strings are anagrams.
"); else printf("The strings are not anagrams.
"); return 0; }
String 1: listen
String 2: silent
The strings are anagrams.

Example 2: Multiple Test Cases

This example tests multiple string pairs to demonstrate both positive and negative cases −

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

int check_anagram(char a[], char b[]) {
    int freq[26] = {0}, i;
    
    if (strlen(a) != strlen(b))
        return 0;
    
    /* Count characters in first string */
    for (i = 0; a[i] != '\0'; i++) {
        if (a[i] >= 'a' && a[i] <= 'z')
            freq[a[i] - 'a']++;
    }
    
    /* Subtract characters from second string */
    for (i = 0; b[i] != '\0'; i++) {
        if (b[i] >= 'a' && b[i] <= 'z')
            freq[b[i] - 'a']--;
    }
    
    /* Check if all frequencies are zero */
    for (i = 0; i < 26; i++) {
        if (freq[i] != 0)
            return 0;
    }
    return 1;
}

int main() {
    char test_pairs[][2][20] = {
        {"abcd", "dcba"},
        {"programming", "gramming"},
        {"earth", "heart"},
        {"hello", "bello"}
    };
    
    int num_tests = sizeof(test_pairs) / sizeof(test_pairs[0]);
    
    for (int i = 0; i < num_tests; i++) {
        printf("Test %d: '%s' and '%s' - ", i+1, test_pairs[i][0], test_pairs[i][1]);
        if (check_anagram(test_pairs[i][0], test_pairs[i][1]))
            printf("Anagrams
"); else printf("Not anagrams
"); } return 0; }
Test 1: 'abcd' and 'dcba' - Anagrams
Test 2: 'programming' and 'gramming' - Not anagrams
Test 3: 'earth' and 'heart' - Anagrams
Test 4: 'hello' and 'bello' - Not anagrams

Key Points

  • Time Complexity: O(n) where n is the length of strings
  • Space Complexity: O(1) using fixed-size frequency array
  • Always check string lengths first − different lengths cannot be anagrams
  • Handle only lowercase letters for simplicity, or extend for uppercase/special characters

Conclusion

Anagram checking in C is efficiently implemented using character frequency counting with arrays. This approach provides optimal time complexity and works reliably for determining if two strings contain the same characters with identical frequencies.

Updated on: 2026-03-15T14:09:31+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements