1

My program checks if two words are anagrams. I'm having trouble setting its value to 0. I've managed to solve the problem by using a for cycle to set all values to 0, but I would like to know if I could use int counts[26] = {0}; instead, obviously not without modifications because the compiler shows error:

8 C:\Dev-Cpp\Templates\anagrams_functions.c 'counts' redeclared as different kind of symbol

Here's the code:

#include <stdio.h>
#include <ctype.h>

void read_word(int counts[26])
{
    int i;
    char ch, let[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', 'X', 'Y', 'Z'};
    int counts[26] = {0};

    while((ch = toupper(getchar())) != '\n'){
        for(i = 0; i < 26; i++){
            if (let[i] == ch)
                counts[i]++;
        }
    }
}

int equal_array(int counts1[26], int counts2[26])
{
    int i;
    for(i = 0; i < 26; i++){
        if(counts1[i] != counts2[i])
            return 0;
        else continue;
    }
    return 1;
}

int main(void)
{
    int counts1[26] = {0};
    int counts2[26] = {0};

    printf("Enter first word: ");
    read_word(counts1);

    printf("Enter second word: ");
    read_word(counts2);

    if(equal_array(counts1, counts2))
        printf("The words are anagrams");
    else
        printf("The words are not anagrams");

    getch();
    return 0;
}
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Matt
  • 144
  • 15

3 Answers3

1

In the function,

void read_word(int counts[26])

you declare array counts again,

int counts[26] = {0};

You need to change the array counts here to a different name.

By reading your source code, I suggest that you remove the declaration of counts in the function.

Deidrei
  • 2,125
  • 1
  • 14
  • 14
  • Thanks man that solved it. By the way, I first changed counts to countsx, without changing it in the incrementation. A mistake, but the program works out just fine. I think it's because I don't even need to set its values to zero, it already is from counts1 or counts2, because, being arguments in read_word's call on main, their value is considered on counts. I took off the declaration and it worked, just thought I should say it. But I noticed this thanks to you, so, I'm grateful. – Matt Dec 16 '13 at 02:46
  • Changing the name of the variable breaks the function. `counts` is an out-parameter. It needs to be cleared before the main loop. If you just declare a variable with a different name, then `counts` won't get cleared. If you change the name and change the reference in the main loop, the function won't produce any output. – zwol Dec 16 '13 at 02:48
  • @Zack: I just helped the OP to solve compiling error. By your comment, I suggested him to remove the array declaration. – Deidrei Dec 16 '13 at 02:59
  • Yeah you really need to read the entire program for bugs before answering. – zwol Dec 16 '13 at 03:09
1

You're declaring counts twice in this function, once as a formal argument and once as a local variable.

void read_word(int counts[26])
{
    /* Code */
    int counts[26] = {0};
    /* Code */
}

There's two things you can do to clear counts. First, is doing what you're doing now, having the caller clear it:

int main(void)
{
    int counts1[26] = {0};
    int counts2[26] = {0};
    /* Code */
}

The second is to have the callee clear counts. memset is perfect for this:

void read_word(int counts[26])
{
    /* Code */
    memset(counts, 0, sizeof(counts));
    /* Code */
}
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
  • Hey, I tried using memset, but the program does not identify anagrams. Its output is always that they are not. When did you learn memset, I mean, at what point of your C study. I'm interested, never seen it. But I'm just a begginner, so... – Matt Dec 16 '13 at 02:56
  • @user3091996 `memset` is used to set all the elements in `counts` to `0`. – Fiddling Bits Dec 16 '13 at 02:58
  • I took of the designator for both counts1 and counts2, because I would be using the call to clear counts. Did you do that as well? Now my main starts with their declarations but without any specific value. – Matt Dec 16 '13 at 03:05
0

You have counts both as a read_word() function parameter and as a variable inside this function.

Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31