1172: 零起点学算法79——统计单词个数

系统管理员 2022-10-24 05:48 254阅读 0赞

Description

输入一行字符(少于300个字符),以回车结束,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。

Input

输入一字符串,以回车结束。

Output

输出该字符串中单词的个数

" class="reference-link">Sample Input 5f8f312f51791b8bb0ed0ae07c2ffa43.gif

  1. This is a c program.

Sample Output

  1. 5

Source

零起点学算法

Code

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. using namespace std;
  5. int main()
  6. {
  7. char str[500];
  8. int sum=0;
  9. gets(str);
  10. int n=strlen(str);
  11. for(int i=1;i<n;i++)
  12. {
  13. if((str[i-1]==' ')&&(str[i]!=' '))
  14. sum++;
  15. }
  16. cout<<sum+1<<endl;
  17. }

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读

    相关 单词个数统计

    问题描述   编写一个程序,输入一个字符串(长度不超过80),然后统计出该字符串当中包含有多少个单词。例如:字符串“this is a book”当中包含有4个单词。