统计字符串中单词的个数
2016-02-10 18:56
429 查看
Description
输入一行字符(少于300个字符),以回车结束,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。Input
输入一字符串,以回车结束。Output
输出该字符串中单词的个数Sample Input
This is a c program.
Sample Output
5
#include<cstdio> #include<cstdlib> #include<cstring> int main() { char ch; int _count = 0; int word = 0; while((ch = getchar()) != '\n')//出现TLE的原因:数据里面没有出现换行符就结束了 { if(ch == EOF)//加这行之前 出现TLE break; else { if(ch == ' ') word = 0; else if(word == 0) { word = 1; _count++; } } } printf("%d\n" , _count); return 0; }
#include<cstdio> #include<cstdlib> #include<cstring> char str[305]; int main() { while(gets(str) != NULL) { int _count = 0; int word = 0; for(int i = 0 ; str[i] != '\0' ; i++) { if(str[i] == ' ') word = 0; else if(word == 0) { _count++; word = 1; } } printf("%d\n" , _count); } return 0; }
相关文章推荐
- 在Linux机上配置Eclipse并部署Maven
- 自己做题的简单的算法
- Jquery封装之----事件绑定(一)
- spring(AOP)静态代理、JDK动态代理、cglib实现代理
- 函数指针&绑定: boost::functoin/std::function/bind
- SendMessage和PostMessage
- 更新时提示“windows update 更新错误代码8000FFF 无法更新”。
- Debian常用软件
- 斐波那契数列
- JavaScript高级程序设计学习笔记第十章--DOM
- Eclipse使用
- scrapy爬虫代理——利用crawlera神器,无需再寻找代理IP
- hdu5233 邻接表 两组代码,一组AC,一组不知道为什么RE了(以后再看)
- Xcode 【Asset Catalog】
- 使用Dom4j时遇到的两个问题
- 初涉网络,自己对服务器的一些基础理解
- hdu acm 1869 六度分离
- 工具篇 - 如果让你重新做一款APP
- 常见的反爬虫和应对方法
- Debian常用設置