Search code examples
cstrcmpansi-c

Using the strcmp() function in C


I am writing a program that is supposed to input from the command, and then find the word frequency of the input. I am having trouble with comparing strings (char arrays) using the strcmp() function. I have been at it for hours, but I still don't understand what I am doing wrong. Does it have to do with pointers? Here my my code:

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

int main(){
    char Words[501][21];
    int FreqNumbers[500];
    char temp[21] = "zzzzz";
    char Frequency[5][21];
    int wordCount = 0;
    int numberCount = 0;
    int i = 0;
    int counter = 0;
    int end = 0;

    do {
        scanf("%20s",Words[wordCount]);
        for(counter = 0;counter < wordCount;counter++){
            if(wordCount > 0){
                if(strcmp(Words[wordCount], Words[counter]) == 0){
                    FreqNumbers[counter]++;
                    break;
                }
                FreqNumbers[wordCount]++;
            }
        }
        wordCount++;
        printf("%s", Words[wordCount - 1]);
        printf("%s", temp);
    } while(strcmp(Words[wordCount],&temp) != 0);

    return(0);
}

Solution

  • The strcmp function, instead of comparing the word entered by the user with "zzzzz", it was checking the next entry in the array with "zzzzz" And so it was not terminating as there was never a match. (As you do wordCount++; before the strcmp function)

    char temp[10] - An array of 10 chars which temp will be pointing to. (immutable/constant).

    You are passing the strcmp function, the address of the variable which points to a memory while it should be given a pointer to the memory.(little confusing but i hope you get the picture). so ideally speaking it should be given.

    strcmp(Words[wordCount],temp); or strcmp(Words[wordCount],&temp[0]);

    Although whatever I said might be a little bit confusing. I would highly recommend you to see KnR and read on arrays especially array of chars

    I have made some changes to your code. It is working as required now. Pl have a look and mark as an answer if acceptable

    #include <stdio.h>
    #include <string.h>
    
    int main(){
    
        char Words[501][21]={{0}};          //zero initialized
        char *temp = "zzzzz";       //string literal
        int FreqNumbers[500],wordCount = 0,counter = 0;     //other variables
    
        do {
    
            scanf("%s",Words[wordCount]);
    
            for(counter = 0;counter < wordCount;counter++){
    
                if(wordCount > 0){
                    if(strcmp(Words[wordCount], Words[counter]) == 0){
                        FreqNumbers[counter]++;
                        break;
                    }
                    FreqNumbers[wordCount]++;
                }
            }
            wordCount++;
            printf("%s\n", Words[wordCount - 1]);           //print if required
            printf("%s\n", temp);                           //print if required
    
        } while(strcmp(Words[wordCount-1],temp) != 0);      
    
        return(0);
    }