English 中文(简体)
使用 C 中的strcmp () 函数
原标题:Using the strcmp() function in C

我正在写一个程序, 它应该从命令中输入, 然后找到输入的单词频率 。 我无法使用 strcmp () 函数比较字符串( 字符阵列) 。 我已经在程序上待了好几个小时, 但我仍然不明白自己做错了什么 。 这是否与指针有关? 我的代码在这里:

#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);
}
最佳回答

strcmp 函数, 而不是将用户输入的单词与“ zzzzzz” 比较, 而是用“ zzzzzz” 检查数组中的下一个条目, 因此它没有终止, 因为从来就没有匹配过 。 (正如您所做的那样, wordCount+++; before the strcmp 函数)

chart temp[10] - 由10个字符组组成的阵列, temp 将指向这些字符组。 (不可替换/恒定)。

您正在通过 strcmp 函数, 变量的地址指向一个内存, 而该变量应该给内存一个指针。 (略为混淆, 但希望您能看到图片) 因此最好该用语句表达 。

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

尽管我所说的可能有点混淆。 我强烈建议您查看 < code> KnR , 并阅读数组, 特别是 < code> alary of chars 。

我对您的代码做了一些修改, 它正在按要求运作。 Pl 的外观和标记是答案, 如果可以的话

#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
", Words[wordCount - 1]);           //print if required
        printf("%s
", temp);                           //print if required

    } while(strcmp(Words[wordCount-1],temp) != 0);      

    return(0);
}
问题回答
while(strcmp(Words[wordCount],&temp) != 0);

temp 已经是一个 const chari {/code> 。 不要使用 amp; 运算符。 这将给予您一个连接字符指针的指针 。

do {
    scanf("%20s",Words[wordCount]);
    wordCount++;

} while(strcmp(Words[wordCount],&temp) != 0);

你只是在这里要求一个钉子,你为什么在圈圈里做这个?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...