输入一行字符,统计其中分别有多少个单词和空格。比如输入:"How are you",有3个单词和2个空格。
每行输入一字符串
输出单词个数及空格个数,用逗号隔开
#include<stdio.h>
#include<string.h>
int main(void)
{
char ch[256];
while (gets(ch) != NULL)
{
int word=0, blank = 0,i=0,flag=0;
int len = strlen(ch);
while (ch[i] != '\0')
{
if (ch[i] == ' ')
{
blank++;
flag = 0;
}
else if (flag == 0)
{
word++;
flag = 1;
}
i++;
}
printf("%d,%d\n", word, blank);
}
return 0;
}