1. 计算字符串中的word的个数
int countNumWords(const char *str) {
bool inWord = false;
int wordCount = 0;
while (*str) {
if (!inWord && isalpha(*str)) {
inWord = true;
wordCount++;
}
else if (inWord && *str == ' ') {
inWord = false;
}
str++;
}
return wordCount;
}
2. 删除字符串中的空格
void removeSpace(char *str) {
char *p1 = str, *p2 = str;
do {
while (*p2 == ' ')
p2++;
} while (*p1++ = *p2++);
}