int countSegments(char* s)
{
int count = 0;
int i = 0;
if (strlen(s) == 0)
{
return 0;
}
for (; s[i] != '\0'; i++)
{
if (s[i] != ' ' && s[i + 1] == ' ')
{
count++;
}
}
if (s[i - 1] != ' ' && count == 0)//只有一个单词的时候
{
return 1;
}
else if (s[strlen(s) - 1] != ' ')//字符串末尾没有空格
{
return count + 1;
}
else
return count;
}
int main()
{
char a[] = "hallo";
printf("%d",countSegments(a));
return 0;
}
11-27
1101
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-18
1188
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)