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;
}
统计字符串中的单词个数(C语言)
最新推荐文章于 2024-03-30 20:18:55 发布