An anagram is a word or phrase formed by rearranging the letters of another, such as “listen” and “silent.” Checking for anagrams is a common problem in programming because it combines string manipulation, character counting, and logic. Understanding how to detect anagrams helps beginners strengthen their skills in arrays, loops, and conditional statements in C.

with hands-on learning.
get the skills and confidence to land your next move.
Anagram checking is widely used in applications like word games, text analysis, and even in some encryption techniques. Writing a C program for this purpose allows beginners to learn how to handle strings efficiently, compare character counts, and implement logic that can be applied to real-world programming problems. In this article, we’ll explore multiple beginner-friendly programs to check if two strings are anagrams, from simple predefined examples to user input and function-based approaches.
Program 1: Check Anagram Strings Using Sorting
This program checks whether two strings are anagrams by sorting their characters and comparing the sorted results. It’s one of the simplest ways to check for anagrams and helps beginners understand how rearranging letters can reveal whether two words are made up of the same characters.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// Function to compare two characters (case-insensitive)
int compareChar(const void *a, const void *b) {
char c1 = tolower(*(const char *)a);
char c2 = tolower(*(const char *)b);
return c1 - c2;
}
int main() {
char str1[100], str2[100];
printf("Enter the first string: \n");
scanf("%s", str1);
printf("Enter the second string: \n");
scanf("%s", str2);
// Check if lengths are equal
if (strlen(str1) != strlen(str2)) {
printf("Strings are not anagrams.\n");
return 0;
}
// Sort both strings (case-insensitive)
qsort(str1, strlen(str1), sizeof(char), compareChar);
qsort(str2, strlen(str2), sizeof(char), compareChar);
// Compare sorted strings (case-insensitive)
if (strcasecmp(str1, str2) == 0)
printf("The strings are anagrams.\n");
else
printf("The strings are not anagrams.\n");
return 0;
}
In this program, both strings are sorted alphabetically using the qsort()
function from the C Standard Library. The compareChar()
function defines how the sorting should be done — it compares two characters at a time. Once sorted, if both strings become identical, they are anagrams. This approach is easy to understand but takes O(n log n)
time because of sorting.
Program 2: Check Anagram Using Frequency Array
This program checks if two predefined strings are anagrams using a frequency array, which is one of the most efficient methods for this problem. It’s ideal for beginners who want to understand how character frequency can be used to compare strings quickly and accurately.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[100];
char str2[100];
int count[256] = {0};
int i;
printf("Enter the first string: \n");
scanf("%s", str1);
printf("Enter the second string: \n");
scanf("%s", str2);
for(i = 0; str1[i] && str2[i]; i++) {
count[tolower(str1[i])]++;
count[tolower(str2[i])]--;
}
int flag = 1;
for(i = 0; i < 256; i++) {
if(count[i] != 0) {
flag = 0;
break;
}
}
if(flag)
printf("\"%s\" and \"%s\" are anagrams.\n", str1, str2);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", str1, str2);
return 0;
}
In this program, a frequency array of size 256 (representing all possible ASCII characters) keeps track of character occurrences. Each character in the first string increases the frequency count, while the same character in the second string decreases it. When all values in the array return to zero, it confirms that both strings have identical character distributions, making them anagrams. This method runs in O(n + m) time and uses constant space, making it faster and more memory-efficient than sorting-based approaches.
Program 3: Check Anagram Using a Function
Using a function to check anagrams makes the code reusable and organized. This approach is useful when checking multiple string pairs.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isAnagram(char str1[], char str2[]) {
int count[256] = {0};
int i;
if(strlen(str1) != strlen(str2))
return 0;
for(i = 0; str1[i] && str2[i]; i++) {
count[tolower(str1[i])]++;
count[tolower(str2[i])]--;
}
for(i = 0; i < 256; i++) {
if(count[i] != 0)
return 0;
}
return 1;
}
int main() {
char str1[100], str2[100];
printf("Enter first string: \n");
scanf("%s", str1);
printf("Enter second string: \n");
scanf("%s", str2);
if(isAnagram(str1, str2))
printf("\"%s\" and \"%s\" are anagrams.\n", str1, str2);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", str1, str2);
return 0;
}
The isAnagram()
function encapsulates the logic for frequency counting. The main function handles input and output. Beginners can see how functions simplify code, make it reusable, and separate logic from interface, which is a good programming practice.
Program 4: Space-Handling Anagram Check
This improved version focuses on ignoring spaces in the input strings. By skipping spaces while counting characters, the program can correctly identify anagrams even when words are separated by spaces, making it more practical for real-world text input.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[100], str2[100];
int count[256] = {0};
int i;
printf("Enter first string:\n");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline
printf("Enter second string:\n");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline
// Count characters in first string, ignoring spaces
for (i = 0; str1[i] != '\0'; i++) {
if (str1[i] != ' ') // skip spaces
count[tolower(str1[i])]++;
}
// Subtract characters in second string, ignoring spaces
for (i = 0; str2[i] != '\0'; i++) {
if (str2[i] != ' ') // skip spaces
count[tolower(str2[i])]--;
}
// Check if all counts are zero
for (i = 0; i < 256; i++) {
if (count[i] != 0) {
printf("Strings are not anagrams.\n");
return 0;
}
}
printf("Strings are anagrams.\n");
return 0;
}
This program ignores all spaces while comparing characters, so strings like "Dormitory"
and "Dirty room"
are correctly recognized as anagrams. It’s a simple but practical improvement for handling realistic text input and makes the program more flexible and user-friendly.
Frequently Asked Questions (FAQ)
Here are some common questions beginners have about checking anagram strings in C.
Q1. What is an anagram in programming?
An anagram is when two strings contain the same characters in any order, such as “listen” and “silent”. In programming, it means rearranging the letters of one string to form another.
Q2. Why do we use count[]
arrays?
The count array keeps track of character frequency. Matching counts ensure both strings contain the same characters.
Q3. Why is sorting an easy method for checking anagrams?
Sorting arranges letters alphabetically, so identical character sets produce identical sorted strings, simplifying comparison.
Q4. Why check the length of strings first?
Strings of different lengths cannot be anagrams, so checking length saves unnecessary computation.
Q5. Can this program handle spaces?
The earlier versions don’t ignore spaces. However, Program 4 solves this by ignoring spaces or punctuation marks using isalpha()
.
Q6. Why use a function for checking anagrams?
Functions improve code readability, reusability, and organization, especially when checking multiple string pairs.
Conclusion
Checking anagram strings in C is a practical and beginner-friendly exercise that helps develop skills in string handling, arrays, loops, conditional logic, and functions. From using predefined strings to interactive programs and function-based approaches, each example teaches how to implement logic efficiently and apply it in real-world scenarios like word games or text analysis. Practicing these programs will strengthen your understanding of string manipulation and problem-solving in C.
Additional & References
To further enhance your C programming skills and explore string manipulation exercises, the following resources are very useful:
- C Standard Library (string.h) – Documentation for string handling functions like
strlen()
andstrcmp()
, essential for string manipulation. - Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
- GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
- Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.