📖 Scenario: You are working on a word game app where players enter two words. You want to check if these two words are anagrams, meaning they have the same letters in a different order.For example, "listen" and "silent" are anagrams.
🎯 Goal: Build a simple C program that checks if two given words are anagrams by comparing their sorted letters.
📋 What You'll Learn
Create two character arrays to hold the words
Create a helper variable for word length
Write a function to sort the characters of a word
Compare the sorted words to check if they are anagrams
Print the result as 'Anagrams' or 'Not Anagrams'
💡 Why This Matters
🌍 Real World
Anagram checks are useful in word games, spell checkers, and cryptography to find matching words or detect rearrangements.
💼 Career
Understanding string manipulation and sorting algorithms is fundamental for software development, especially in text processing and algorithm design roles.
Progress0 / 4 steps
1
DATA SETUP: Create two words
Create two character arrays called word1 and word2 with the exact values "listen" and "silent" respectively.
DSA C
Hint
Use char word1[] = "listen"; and char word2[] = "silent"; to create the words.
2
CONFIGURATION: Create a variable for word length
Create an integer variable called length and set it to the length of word1 using sizeof(word1) - 1.
DSA C
Hint
Use int length = sizeof(word1) - 1; to get the length excluding the null character.
3
CORE LOGIC: Write a function to sort characters and check anagrams
Write a function called sort_chars that takes a character array and its length, and sorts the characters using a simple bubble sort. Then, in main, call sort_chars on word1 and word2, compare them using strcmp, and store the result in an integer variable called result.
DSA C
Hint
Use nested loops to compare and swap characters in sort_chars. Use strcmp to compare sorted words.
4
OUTPUT: Print if words are anagrams
Add a printf statement inside main to print "Anagrams" if result is 0, otherwise print "Not Anagrams".
DSA C
Hint
Use if (result == 0) to check and printf("Anagrams\n") or printf("Not Anagrams\n") accordingly.
Practice
(1/5)
1. What does it mean if two strings are anagrams of each other?
easy
A. They contain the same characters in any order
B. They have the same length but different characters
C. They start with the same character
D. They end with the same character
Solution
Step 1: Understand the definition of anagrams
Anagrams are words or phrases made by rearranging the letters of another, so they must have the same characters.
Step 2: Compare the character sets
If two strings have exactly the same characters, just in different orders, they are anagrams.
Final Answer:
They contain the same characters in any order -> Option A
Quick Check:
Anagrams = same characters [OK]
Hint: Anagrams have identical letters, order doesn't matter [OK]
Common Mistakes:
Thinking length alone defines anagrams
Assuming same start or end means anagram
Ignoring character counts
2. Which of the following C code snippets correctly checks if two strings are anagrams by sorting?
easy
A. Check only first and last characters
B. Compare lengths only
C. Use strcmp directly without sorting
D. Sort both strings and compare with strcmp
Solution
Step 1: Understand sorting approach
Sorting both strings puts their characters in order, so if they are anagrams, sorted strings will be identical.
Step 2: Use strcmp to compare sorted strings
strcmp returns 0 if strings are equal, confirming anagram status.
Final Answer:
Sort both strings and compare with strcmp -> Option D
Quick Check:
Sort + strcmp = correct anagram check [OK]
Hint: Sort strings then compare to check anagrams [OK]
Common Mistakes:
Comparing strings without sorting
Ignoring length check before sorting
Using strcmp on unsorted strings
3. What will be the output of the following C code snippet?
#include
#include
#include
bool areAnagrams(char *str1, char *str2) {
int count[256] = {0};
int i;
for (i = 0; str1[i] && str2[i]; i++) {
count[(unsigned char)str1[i]]++;
count[(unsigned char)str2[i]]--;
}
if (str1[i] || str2[i]) return false;
for (i = 0; i < 256; i++) {
if (count[i] != 0) return false;
}
return true;
}
int main() {
if (areAnagrams("listen", "silent"))
printf("Anagrams\n");
else
printf("Not Anagrams\n");
return 0;
}
medium
A. Anagrams
B. Not Anagrams
C. Compilation error
D. Runtime error
Solution
Step 1: Analyze character counting logic
The function increments count for each char in str1 and decrements for str2. If strings are anagrams, counts balance to zero.
Step 2: Check string lengths and counts
Strings "listen" and "silent" have same length and characters, so count array ends with all zeros, returning true.
Final Answer:
Anagrams -> Option A
Quick Check:
Balanced counts = anagrams [OK]
Hint: Count chars increment/decrement; zero counts mean anagrams [OK]
Common Mistakes:
Not checking string length equality
Ignoring unsigned char cast for indexing
Returning true without count check
4. Identify the bug in this C function intended to check anagrams:
bool isAnagram(char *s1, char *s2) {
int count[26] = {0};
for (int i = 0; s1[i]; i++) {
count[s1[i] - 'a']++;
count[s2[i] - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (count[i] != 0) return false;
}
return true;
}
medium
A. Missing semicolon in the first for loop
B. Uses int for loop variable instead of size_t
C. Does not check if strings have same length
D. Second loop starts from i=1 instead of i=0
Solution
Step 1: Check loop and length conditions
The loop runs only while s1[i] is valid, but s2 may be shorter or longer, causing mismatch.
Step 2: Identify missing length check
Without verifying s1 and s2 have same length, function may incorrectly return true or access invalid memory.
Final Answer:
Does not check if strings have same length -> Option C
Quick Check:
Length check missing = bug [OK]
Hint: Always check string lengths before counting chars [OK]
Common Mistakes:
Assuming strings are always same length
Ignoring null terminator in second string
Not handling uppercase letters (less critical here)
5. Given two strings that may contain uppercase, lowercase, and spaces, which approach best checks if they are anagrams ignoring case and spaces?
hard
A. Sort strings directly including spaces and compare
B. Convert both to lowercase, remove spaces, then count characters
C. Compare lengths including spaces only
D. Check only first and last characters ignoring case
Solution
Step 1: Normalize strings by case and spaces
Convert all letters to lowercase and remove spaces to compare only meaningful characters.
Step 2: Count characters or sort normalized strings
After normalization, counting characters or sorting will correctly identify anagrams ignoring case and spaces.
Final Answer:
Convert both to lowercase, remove spaces, then count characters -> Option B
Quick Check:
Normalize case and spaces before checking [OK]
Hint: Normalize case and remove spaces before checking anagrams [OK]
Common Mistakes:
Comparing strings with spaces included
Ignoring case differences
Assuming length check includes spaces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.