问题描述:
(1)阅读下面的程序,完成类似的字符统计功能
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[50];
int i=0,n=0;
cout<<"输入字符串:";
gets(str);
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9') n++;
i++;
}
cout<<"其中的数字个数是: "<<n<<endl;
return 0;
}
请分别编制程序,完成下面的处理(选 2个):
统计字母'A'出现的次数;
统计字符串中(大/小写)字母个数;
统计每一个数字字符出现的次数;
提示:可以定义一个数组 int a[10],保存’0’-’
9’在字符串中出现的次数(例 a[0]表示字符’0’的
个数)
统计每一个字母出现的个数
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[1000];
int i=0,n=0,c=0,d=0,e=0;
cout<<"输入字符串:";
gets(str);
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9') n++;
if (str[i]=='A') c++;
if(str[i]>='A'&&str[i]<='Z') d++;
if(str[i]>='a'&&str[i]<='z') e++;
i++;
}
cout<<"其中的数字个数是: "<<n<<endl;
cout<<"其中的字母A个数是: "<<c<<endl;
cout<<"其中的字母个数是:"<<d+e<<endl;
cout<<"大写"<<d<<"个 小写"<<e<<"个"<<endl;
return 0;
}
运行结果: