Search code examples
canagram

Anagram Solver, array[26] not working correctly


I've nearly finished my anagram solver program where I input two strings and get the result of whether they are anagrams of each other. For this example i'm using 'Payment received' and 'Every cent paid me'.

The problem i'm getting is when I output the letterCount arrays, letterCount1 is incorrect (it doesn't think there is a character 'd' but there is.) but letterCount2 is correct.

Can anyone see a problem with this because i'm completely baffled?

#include <stdio.h>
#include <string.h>

int checkAnagram(char string1[], char string2[])
{
        int i;
    int count = 0, count2 = 0;
    int letterCount1[26] = {0};
    int letterCount2[26] = {0};

    for(i = 0; i < strlen(string1); i++)
    {
        if(!isspace(string1[i]))
        {
            string1[i] = tolower(string1[i]);
            count++;
        }
    }

    for(i = 0; i < strlen(string2); i++)
    {
        if(!isspace(string2[i]))
        {
            string2[i] = tolower(string2[i]);
            count2++;
        }
    }

    if(count == count2)
    {
        for(i = 0; i < count; i++)
        {
            if(string1[i] >='a' && string1[i] <= 'z')
            {
                letterCount1[string1[i] - 'a'] ++;
            }

            if(string2[i] >='a' && string2[i] <= 'z')
            {
                letterCount2[string2[i] - 'a'] ++;
            }
        }

        printf("%s\n", string1);

        for(i = 0; i < 26; i++)
        {
            printf("%d ", letterCount1[i]);
            printf("%d ", letterCount2[i]);
        }
    }
}

main()
{
    char string1[100];
    char string2[100];

    gets(string1);
    gets(string2);

    if(checkAnagram(string1, string2) == 1)
    {
        printf("%s", "Yes");
    } else
    {
        printf("%s", "No");
    }
}

Solution

  • I fixed it for you:

    #include <stdio.h>
    #include <string.h>
    
    int checkAnagram(char string1[], char string2[])
    {
        int i;
        int count = 0, count2 = 0;
        int letterCount1[26] = {0};
        int letterCount2[26] = {0};
        int len1 = strlen(string1);
        int len2 = strlen(string2);
    
        for(i = 0; i < len1; i++)
        {
            if(!isspace(string1[i]))
            {
                string1[i] = tolower(string1[i]);
                count++;
            }
        }
    
        for(i = 0; i < len2; i++)
        {
            if(!isspace(string2[i]))
            {
                string2[i] = tolower(string2[i]);
                count2++;
            }
        }
    
        if(count == count2)
        {
            for (i=0; i<len1; i++)
                if (!isspace(string1[i]))
                    letterCount1[string1[i]-'a']++;
            for (i=0; i<len2; i++)
                if (!isspace(string2[i]))
                    letterCount2[string2[i]-'a']++;
    
            int flag = 1;
            for(i = 0; flag && i < 26; i++)
                if (letterCount1[i] != letterCount2[i])
                    flag = 0;
            return flag;
        }
        return 0;
    }
    
    main()
    {
        char string1[100];
        char string2[100];
    
        gets(string1);
        gets(string2);
    
        if(checkAnagram(string1, string2) == 1)
        {
            printf("%s", "Yes");
        } else
        {
            printf("%s", "No");
        }
    }
    

    First, don't calculate an string's length inside a loop. I extracted them into len1 and len2 variables.

    Second, your loop was wrong! You shouldn't go up to count, you should go up to that string's length.

    Third, you didn't return anything from checkAnagram function.