leetcode 434. Number of Segments in a String

来源:互联网 发布:syslog 数据库 编辑:程序博客网 时间:2024/06/05 09:29

原题:

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) {\    if(strlen(s)==0)        return 0;    int result=0;    for(int n=0;n<strlen(s)-1;n++)    {        if(*(s+n)!=' '&&*(s+n+1)==' ')            result++;    }    if(*(s+strlen(s)-1)!=' ')        result++;    return result;}
这个就是个找特征。。。