int countSegments(char * s)
{
int count = 0;
int len = 0, i = 0;
len = strlen(s);
for(i = 0; i < len; i++)
{
if((i == 0 || s[i - 1] == ' ') && s[i] != ' ')
{
count++;
}
}
return count;
}
1.当前位置不是空格,但上一个位置是空格加1计数;
2.注意s[0]时的判断