习题6-8 统计一行文本的单词个数 (15分)
代码:
#include <stdio.h>
int main()
{
char s[1024];
gets(s);
int i = 0, count = 0;
while (s[i] == ' ') i++;
for (; s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i - 1] != ' ') count++;
}
if (s[i - 1] != ' ') count++;
printf("%d", count);
return 0;
}