The biggest problem is with your concept of Anagram. The classic example is:
"anagram"
"nag a ram"
Each uses the same letters exactly once with whitespace ignored.
There are several ways to approach determining in two strings are an anagram. You can either use a single array (generally of 128 integer values initialized all zero to cover all characters in the ASCII character set, or 256 to also cover the Extended-ASCII characters.
With the single array, you simply loop over each string. With the first string for each non-whitespace character you increment the index corresponding to the ASCII value of the character, and for the second string you decrement the value at the index corresponding to the ASCII value of the character. At the end, if each character in each string is used exactly the same number of times -- all array values will be zero which will confirm an anagram.
Using two arrays of the same size (either 128 or 256), you simply loop over the characters in each string and for each non-whitespace character you increment the index corresponding to that characters ASCII value in similar fashion, but when done, you compare whether the two arrays are equal to each other with a loop or memcmp()
.
A short implementation would be:
#include <stdio.h>
#include <ctype.h>
#define NCHARS 256 /* constant to count array size, covers ASCII + extended ASCII */
int isanagram (const char *s1, const char *s2)
{
int count[NCHARS] = {0}; /* counting array, covers all extended ASCII */
for (; *s1; s1++) /* loop over chars in string 1 */
if (!isspace(*s1)) /* if not whitespace */
count[(int)*s1]++; /* add 1 to index corresponding to char */
for (; *s2; s2++) /* loop over chars in string 2 */
if (!isspace(*s2)) /* if not whitespace */
count[(int)*s2]--; /* subtract 1 from index corresponding to char */
for (int i = 0; i < NCHARS; i++) /* loop over counting array */
if (count[i]) /* if any index non-zero, not anagram */
return 0;
return 1; /* all chars used same number of times -> anagram */
}
(NULL
parameter checks omitted above, add for completeness)
To test you could use a simple main()
that reads two strings and then checks the return from the function above. A return of 1
(true) means the words are anagrams, otherwise they are not anagrams. Simple press Enter alone on a line to quit.
int main (void) {
char str1[NCHARS * 4], str2[NCHARS * 4]; /* arrays for string 1 & string 2 */
for (;;) { /* loop continually */
fputs ("\nenter string 1: ", stdout);
if (!fgets (str1, sizeof str1, stdin) || *str1 == '\n') /* EOF or ENTER alone */
break;
fputs ("enter string 2: ", stdout);
if (!fgets (str2, sizeof str2, stdin) || *str2 == '\n') /* EOF or ENTER alone */
break;
printf ("\nwords %s an anagram\n", isanagram (str1, str2) ? "are" : "are not");
}
}
Example Use/Output
$ ./bin/anagram_count_array
enter string 1: anagram
enter string 2: nag a ram
words are an anagram
enter string 1: anagram
enter string 2: naag a ram
words are not an anagram
enter string 1: cat
enter string 2: tac
words are an anagram
enter string 1: cat
enter string 2: a t c
words are an anagram
enter string 1:
Look things over and let me know if you have questions.