1091

1091: 统计单词

Time Limit: 1 SecMemory Limit: 128 MB

Submit: 2619Solved: 1175

[Submit][Status][Web Board]

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;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容