MrSir231
MrSir231

Reputation: 7

Checking if all elements in array are zero

I'm having trouble determining if two words entered are anagrams.

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

int main() {
    char ch;
    int letter_count[26] = {0};
    int i;
    int sum = 0;

    printf("Enter first word: ");
    do
    {
       scanf("%c", &ch);
       letter_count[ch - 'a']++;
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
      printf("%d ", letter_count[i]);
    printf("\n");

    printf("Enter second word: ");
    do
    {
       scanf("%c", &ch);
       letter_count[ch - 'a']--;
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
        printf("%d ", letter_count[i]);
    for(i = 0; i < 26; i++)
        if(letter_count[ch] != 0)
            sum++;

    if (sum == 0)
        printf("anagrams");
    else
        printf("not anagrams");
}

I have to use the do while part of the code. I can enter the two words, and it prints out the elements in the array, so that "Mattress" and "Smartest" together would have all the elements be zero. However, I'm having trouble with the last part, which is to use a third loop to check whether all the elements are zero.

I figured I could declare an int before hand and have it increment whenever an element wasn't zero, and I could just have any sum greater than zero not be an anagram. However, it always prints out anagram for me.

Upvotes: 0

Views: 4287

Answers (2)

nalzok
nalzok

Reputation: 16107

To handle both upper case and lower case letters, use topper() or to lower() in <ctype.h> to avoid out-of-bound access.

#include <stdio.h>
#include <string.h>
#include <ctype.h> // <---

int main() {
    char ch;
    int letter_count[26] = {0};
    int i;
    _Bool bad = 0;

    printf("Enter first word: ");
    do
    {
        scanf("%c", &ch);
        if(!isalpha(ch)) // <---
        {
            puts("Not a letter");
            continue;
        }
        letter_count[tolower(ch) - 'a']++; // <---
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
        printf("%d ", letter_count[i]);
    printf("\n");

    printf("Enter second word: ");
    do
    {
        scanf("%c", &ch);
        if(!isalpha(ch)) // <---
        {
            puts("Not a letter");
            continue;
        }
        letter_count[tolower(ch) - 'a']--; // <---
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
        printf("%d ", letter_count[i]);
    printf("\n"); // <---

    for(i = 0; i < 26; i++)
        if(letter_count[i] != 0)
        {
            bad = 1;
            break; // <---
        }
    if (bad == 0)
        printf("anagrams");
    else
        printf("not anagrams");
}

Take a look at all places marked // <---.

Upvotes: 0

Mi_Onim
Mi_Onim

Reputation: 412

In your third loop, using letter_count[ch] will not check the entire array. You should iterate through the array using the loop variable i. That part of the code should be:

for (i=0; i<26; i++)
    if (letter_count[i] != 0)
        sum++;

Upvotes: 1

Related Questions