user3337714
user3337714

Reputation: 673

C Program: strcasecmp() not working

I am trying to count the number of occurrences for a word in a dynamic array of struct. But the output is not matching the needed answer. Any guidance or modification to the code will be greatly appreciated.

 void COUNT(struct wordsStruct *allWords, int size)
{
    int boolean = 0;
    int count = 0;

    for (int i = 0; i < size; i++)
    {
        count = 0;
        for (int j = i + 1; j < size; j++)
        {
            printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
            if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
            {
                boolean = 1;
                count++;
            }
            else
            {
                boolean = 0;
            }
        }
        if (boolean == 1 && count != 0)
        {
            allWords[i].number = count;
        }
    }
}

int main(int argc, char** argv)
{
    // Declaring Variables
    FILE *fileReader;
    int numberOfWords;
    struct wordsStruct *container;

    if (argc == 1)
    {
        printf("More Arguments Needed. \n");
    }
    else if (argc == 2)
    {
        numberOfWords = SCAN(&fileReader, argv[1]);
        printf("Number Of Words: %d. \n", numberOfWords);
        container = LOAD(fileReader, numberOfWords, argv[1]);

        qsort(container, numberOfWords, sizeof(struct wordsStruct), compare);

        COUNT(container, numberOfWords);

        for (int i=0; i<numberOfWords; i++)
        {
            printf("WORD: %s. \t", container[i].name);
            printf("Occurence: %d. \n", container[i].number);
        }
    }       
    return (0);
}

    typedef struct wordsStruct
    {
        char* name;
        int number;
    }wordsStruct;

//INPUT FILE
My
Code
ZZZ
ZZZ
Is
Not
zzz
ZZZ
zzz
Working

//Output
WORD: Code.     Occurence: 1. 
WORD: Is.   Occurence: 1. 
WORD: My.   Occurence: 1. 
WORD: Not.  Occurence: 1. 
WORD: Working.  Occurence: 1. 
WORD: Working.  Occurence: 1. 
WORD: zzz.  Occurence: 4. 
WORD: ZZZ.  Occurence: 3.   // THIS SHOULD BE 5?
WORD: zzz.  Occurence: 2. 
WORD: ZZZ.  Occurence: 1. 
WORD: ZZZ.  Occurence: 1.

Upvotes: 0

Views: 1450

Answers (1)

R Sahu
R Sahu

Reputation: 206717

You are erroneously concluding that strcasecmp is not working.

You have some logic errors in your code.

for (int j = i + 1; j < size; j++)
{
    printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
    if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
    {
        boolean = 1;
        count++;
    }
    else
    {
        boolean = 0;
    }
}

The above block of code suffers from:

  1. It doesn't stop after it found a mismatching word.
  2. When the last word in the list doesn't match the word at index i, it sets boolean to 0, which is being used to check whether a match was found or not.

Try this:

for (int i = 0; i < size; i++)
{
   count = 0;
   int j;
   for ( j = i + 1; j < size; j++)
   {
      printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
      if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
      {
         boolean = 1; // Don't need this at all.
         count++;
      }
      else
      {
         break;
      }
   }

   if (count != 0)
   {
      allWords[i].number = count;
   }

   // If there are three matching words, j would be i + 3;
   // We don't need to compare the words at i+1, i+2 again.
   // Change i to be the index of the next word for comparison.
   i = j-1;
}

Update

The part about

allWords[i].number = count;

needs to be better.

for (int i = 0; i < size; i++)
{
   // The count is at least 1
   count = 1;
   int j;
   for ( j = i + 1; j < size; j++)
   {
      printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
      if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
      {
         boolean = 1; // Don't need this at all.
         count++;
      }
      else
      {
         break;
      }
   }

   for ( ; i < j; ++i ) 
   {
      allWords[i].number = count;
   }
}

Upvotes: 1

Related Questions