从终端键盘输入一行字符串,统计其中一共有多少个大写字母和多少个小写字母,并封装成子函数

从终端键盘输入一行字符串,统计其中一共有多少个大写字母和多少个小写字母,并封装成子函数

#include<stdio.h>

#define N 100

void Aa( char str[])

{

int A=0,a=0;

for(int i=0;str[i]!='\0';i++)

{

if((int)str[i]<91)//因为在ASC2码中65~90的符号为大写字母

A++;

else if((int)str[i]>96)//97~122的符号为小写字母

a++;

}

printf("共有大写字母%d个\n共有小写字母%d个\n",A,a);

}

void main()

{

char str[N];

puts("请连续输入100个以内的任意大小写字母:");

gets(str);

Aa(str);

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-07
#include <iostream>
#include <string>
#include <utility>

using namespace std;
typedef pair<int, int> case_type;
case_type caseCnt(const string&);

int main()
{
cout<<"Input:\n";
string str;
getline(cin, str);
case_type res = caseCnt(str);
cout<<"Total upper: "<<res.first<<"\tTotal lower: "<<res.second<<endl;
return 0;
}

case_type caseCnt(const string& str)
{
int ucnt=0, lcnt=0;
for(string::size_type idx = 0; idx != str.size(); ++idx)
{
if(isupper(str[idx]))
++ucnt;
if(islower(str[idx]))
++lcnt;
}
case_type res = make_pair(ucnt, lcnt);
return res;
}本回答被提问者采纳
第2个回答  2011-12-07
用什么编程语言?别白写了。。。
相似回答
大家正在搜