Complete the code to compare the lengths of two strings for anagram check.
if (strlen(str1) [1] strlen(str2)) { return 0; // Not anagrams }
For two strings to be anagrams, their lengths must be equal. If lengths are not equal (using !=), return 0.
Complete the code to initialize the frequency array for counting characters.
int count[256] = {0}; for (int i = 0; i < [1]; i++) { count[(unsigned char)str1[i]]++; count[(unsigned char)str2[i]]--; }
We loop through the length of str1 (which equals str2 length) to count characters.
Fix the error in the condition that checks if all counts are zero after frequency counting.
for (int i = 0; i < 256; i++) { if (count[i] [1] 0) { return 0; // Not anagrams } }
If any count is not zero, the strings are not anagrams, so use '!=' to check.
Fill all three blanks to complete the sorting-based anagram check.
int compare(const void *a, const void *b) {
return (*(char *)[1] - *(char *)[2]);
}
qsort(str1, [3], sizeof(char), compare);The compare function subtracts the characters pointed by 'a' and 'b'. The qsort length is the string length.
Fill all three blanks to complete the final anagram check using sorted strings.
if (strncmp(str1, str2, [1]) [2] 0) { return [3]; // Not anagrams } else { return 1; // Anagrams }
Compare the strings up to their length. If strncmp result is not zero, they differ, so return 0.
