从键盘输入一段字符,以EOF结束,统计其中小写字母、大写字 母、数字和其它字符的个数。尽量用简单的C++语

如题所述

//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;
int main(int argv,char *argc[]){
string s;
char ch;
int upp,low,dig,oth,i;
cout << "Input a string(Ctrl+'z' end)...\n";
while(cin.get(ch))
s+=ch;
for(upp=low=dig=oth=i=0;s[i];i++)
if(s[i]>='A' && s[i]<='Z')
upp++;
else if(s[i]>='a' && s[i]<='z')
low++;
else if(s[i]>='0' && s[i]<='9')
dig++;
else
oth++;
cout << "UPP:\t" << upp << "\nLOW:\t" << low << "\nDIG:\t" << dig << "\nOTH:\t" << oth << endl;
return 0;
}

运行样例:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-14
用ctrl + z结束输入
#include <iostream>
using namespace std;

void main()
{
int s = 0;
int c = 0;
int n = 0;
int o = 0;
char ch;
while (cin >> ch)
{
if (ch >= 'a' && ch <= 'z')
{
s++;
}
else if (ch >= 'A' && ch <= 'Z')
{
c++;
}
else if (ch >= '0' && ch <= '9')
{
n++;
}
else
{
o++;
}
}
cout << "小写字母:" << s << endl;
cout << "大写字母:" << s << endl;
cout << "数字: " << s << endl;
cout << "其他: " << s << endl;
}本回答被提问者采纳
第2个回答  2011-11-14
#include <iostream>
using namespace std;

void main()
{
int s = 0;
int c = 0;
int n = 0;
int o = 0;
char ch;
while (cin >> ch)
{
if (ch >= 'a' && ch <= 'z')
{
s++;
}
else if (ch >= 'A' && ch <= 'Z')
{
c++;
}
else if (ch >= '0' && ch <= '9')
{
n++;
}
else
{
o++;
}
}
cout << "小写字母:" << s << endl;
cout << "大写字母:" << c << endl;
cout << "数字: " << n << endl;
cout << "其他: " << o << endl;
}
相似回答