题目内容:

编写函数统计一个英文字符串(英文句子)中的单词个数。

输入格式:

一个最长500个字母的英文字符串,不包含数字和特殊字符,但可能包含一些英文标点符号(逗号、句点、问号)。标点符号独立出现时不视为一个单词。 单词间可能包含一个或多个空格。

输出格式:

该句子的单词个数

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。

输入样例:

We hope everyone watches them with warmth.

输出样例:

7

#include <iostream>
using namespace std;


int  isLetter(char b);//是否字母

int main()
{    
    char input[500];
    cin.getline(input,500);
    int wordCount=0;
    int i=0;
    int isPreviousLetter=0;
    while(input[i]!='\0'){
        
        if(isLetter(input[i])){
            if(isPreviousLetter==0){
                wordCount++;
            }
            isPreviousLetter=1;
        }else{
            isPreviousLetter=0;
        }
        i++;
    }
    cout<<wordCount;
}



int  isLetter(char a){
    if (a>='a'&&a<='z'){
        return 1;
    }else if (a>='A'&&a<='Z'){
        return 1;
    }else{
        return 0;
    }
}