我需要编写一个程序来读取一个句子并输出句子中的单词数。我已经完成了程序,但问题是我的程序将单词之间的空格计算为字符。如何省略这些空格并仅显示字符串中的单词数?我在想我需要某种类型的循环,但我不知道如何执行它。
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define pause system("pause")
main() {
char mystring[155];
int counter = 0;
printf("Enter your name: ");
scanf("%[^\t\n]", &mystring);
printf("Your name is %s\n", mystring);
// find out the number of characters in the string
counter = strlen(mystring);
printf("There are %i words in the sentence. \n", counter);
// find out how many WORDS are in the sentence. Omit spaces
pause;
} // end of main