#include <stdio.h> #include <string.h> int are_anagrams(char *str1, char *str2) { int len1 = strlen(str1); int len2 = strlen(str2); if (len1 != len2) { return 0; } int map[256] = {0}; for (int i = 0; i < len1; i++) { map[str1[i]]++; map[str2[i]]--; } for (int i = 0; i < 256; i++) { if (map[i] != 0) { return 0; } } return 1; } int main() { char string1[] = "listen"; char string2[] = "silent"; printf("String1: %s\n", string1); printf("String2: %s\n", string2); printf("Are they anagrams? %s\n", are_anagrams(string1, string2) ? "Yes" : "No"); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter