首页 > 试题广场 >

编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输

[问答题]
编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入、输出。
推荐

解: 源程序:

#include <iostream.h>
#include <stdio.h>
int count(char *str)
{
int i,num=0;
for (i=0; str[i]; i++)
{
if ( (str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z') )
num++;
}
return num;
}
void main()
{
char text[100];
cout << "输入一个英语句子:" << endl;
gets(text);
cout << "这个句子里有" << count(text) << "个字母。" << endl;
}

程序运行输出:

输入一个英语句子:

It is very interesting!

这个句子里有 19 个字母。

发表于 2018-04-18 20:45:07 回复(0)