我正在创建一个计算给定句子中单词数量的程序,但是当我运行程序时,每个句子都会给出0。
#include <stdio.h>
#include <string.h>
char wordCount(char sentence[]);
int main(void) {
char wordsToCount[100];
int numberOfWords;
int i;
printf("Word count \n");
printf("========== \n");
for (i=0; wordsToCount[i] != '\0'; i++) {
printf("Text to be analyzed: ");
scanf(" %s", &wordsToCount[i]);
}
/*numberOfWords = */wordCount(wordsToCount);
}
char wordCount(char sentence[]) {
int wordsCount;
int i;
int count = 0;
for (i=0; sentence[i] != '\0'; i++) {
if (sentence[i] == ' ') {
count++;
}
}
printf("Word Count : %d \n", count);
}
让我们说如果我输入: 你好,世界。我应该得到2,但程序一直要求我再次输入数据,但是如果我输入的内容如下: C不是那么难! 我得到0。
Word count
==========
Text to be analyzed: C is not that hard
Word Count : 0
Word count
==========
Text to be analyzed: Hello World
Text to be analyzed: Text to be analyzed: Hello World
Text to be analyzed: Text to be analyzed: