/* Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. Please note that the string does not contain any non-printable characters. Example: Input: "Hello, my name is John" Output: 5 */ int countSegments(char* s) { int i,j,count = 0; for( i = 0 ; i < strlen(s) ; i++) if(s[i]!=32)//找到不是空格的字符,对于这个字符之后进行遍历,知道下一个空格或者回车为一个字符进行统计 for(j=i;;j++) if( s[j]==32 ||s[j]==0)//找到空格或者回车,到这里是一个字符串,进行+1,然后将此处为止返回继续进行查询 { count++; i=j; break; } return count; }
434_Number_of_Segments_in_a_String
原创
©著作权归作者所有:来自51CTO博客作者jenslee的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
psql窗口函数 ROW_NUMBER的应用
psql窗口函数的使用
窗口函数 一对多 表结构 -
STL-常用容器-string
string 类内部封装了很多成员方法例如:查找find,拷贝copy,删除delete 替换replace,插入insertstring管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责(RAII)
C++STL STL常用容器 string类 string类的常用接口 -
1104. Sum of Number Segments (20)
num 输入队列有多少个数依次输入num个in,都是不大于1.0的小数,要求输出这个队列】
C++ ios 测试点 #include -
1104 Sum of Number Segments (20 分) 题意
1104Sum of egme...
i++ 递增子序列 #include -
nginx集群 测试
重点两部分:一、负载均衡二、tomcat集群 所谓tomcat集群,就是可以向外提供并行服务的多台机器,任何一台服务器宕机,其它服务器可以替代它向外提供服务,而不影响用户访问。 nginx是一个常用的反向代理服务,可自定义模块,实现请求转发及负
nginx集群 测试 nginx session tomcat集群 session共享