今天做二级题的时候有一道题要求统计单词个数,答案的思路是统计空格的个数然后加一,如果两个单词中间空格数不确定的话统计将不准确。
下面是简单修改后的版本1.
#include <iostream>
#include <cstring>
using namespace std;
int countword(char* str)
{
int count=0;
int i=0;
while(i< strlen(str))
{
if(('A'<*(str+i) && *(str+i)<'Z')||('a'<*(str+i) && *(str+i)<'z'))
{
while(*(str+i)!=' ') i++;
count++;
i++;
}
}
return count;
}
int main()
{
char* ch="what does the fox say";
cout << countword(ch);
return 0;
}