给定一个字符串,计算其中单词的数量。
比如:“Hello, my name is John.” -> 5
int countNumStr(const char *str)
{
if (str == NULL)
{
return 0;
}
int result = 0;
bool foundWord = false;
while (*str)
{
if (!foundWord && isalpha(*str))
{
foundWord = true;
result++;
}
else if (foundWord && !isalpha(*str))
{
foundWord = false;
}
str++;
}
return result;
}