Read the question, write C on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read two words and print yes if they are anagrams of each other, otherwise no.
Compare case-insensitively. Count letters, or sort both strings and compare.
Input. Two words, whitespace-separated.
Output. yes or no.
Examples
Input
listen silent
Output
yes
Input
hello world
Output
no
Hint
- Count each letter a–z in both words; the two count arrays must match.
- tolower before you index into the count array.
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char a[64], b[64];
if (scanf("%63s %63s", a, b) != 2) return 1;
int ca[26] = {0}, cb[26] = {0};
for (int i = 0; a[i]; i++) {
int c = tolower((unsigned char)a[i]);
if (c >= 'a' && c <= 'z') ca[c - 'a']++;
}
for (int i = 0; b[i]; i++) {
int c = tolower((unsigned char)b[i]);
if (c >= 'a' && c <= 'z') cb[c - 'a']++;
}
int ok = 1;
for (int i = 0; i < 26; i++) if (ca[i] != cb[i]) ok = 0;
printf(ok ? "yes\n" : "no\n");
return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.