c语言输入一串英文编译程序输出共有几个单词

来源:百度知道 编辑:UC知道 时间:2024/05/11 00:20:49

#include <iostream>
using namespace std;

int count(char *sentence)
{
int sum=0;
while(*sentence!='\0')
{
if((*sentence>='A')&&(*sentence<='z'))
{
sum++;
}
sentence++;
}
return sum;
}

void main()
{
char sentence[1024];
cout<<"Please input a sentence. "<<endl;
cin>>sentence;
cout<<"This sentence has "<<count(sentence)<<" words."<<endl;
}