1091: 统计单词
Time Limit: 1 SecMemory Limit: 128 MB
Submit: 2619Solved: 1175
Description
输入一个正整数 repeat (0
输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
Input
见sample
Output
见sample
Sample Input
3
Every night in my dreams
I see you I feel you
That is how I know you go on
Sample Output
5
6
8
代码参考网址:https://blog.csdn.net/sinat_35645479/article/details/53208157
#include<stdio.h>
#include<string.h>
int main(){
int m,rep,count;
char a[100];
scanf("%d",&rep);
getchar();
while(rep--)
{
count=0;
//scanf("%s",a);
gets(a);
for(int i=0;i<strlen(a);i++)
{
while(a[i]==' ')
i++;
if(a[i]!=' ') count++;
while(a[i+1]!=' ') i++;
}
if(a[strlen(a)-1]==' ')count--;//句末若是有空格,要减1
printf("%d\n",count);
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main(){
int m,rep,count;
char a[100];
scanf("%d",&rep);
getchar();
while(rep--)
{
count=0;
//scanf("%s",a);
gets(a);
for(int i=0;i<strlen(a);i++)
{
while(a[i]==' ')
i++;
if(a[i]!=' ') count++;
while(a[i+1]!=' ') i++;
}
if(a[strlen(a)-1]==' ')count--;//句末若是有空格,要减1
printf("%d\n",count);
}
return 0;
}