陆川县米场镇谁钱多:输入一行字符,要求统计出英文字母、数字、空格和其它字符的个数

来源:百度文库 编辑:高校问答 时间:2024/04/30 01:20:29
循环结构程序设计(输入输出采用cin和cout)
帮帮忙啊 ~~ 我很需要这个程序!!!
谢谢T-T

#include <iostream.h>
#include <ctype.h>

const long MAX = 1000;

int main()
{
char str[MAX];
long digitNumber=0, letterNumber=0, blankNumber=0, otherCharNumber=0;
long loopCount;

while(cin.getline(str,MAX), cin.rdstate() == cin.goodbit)
{
for(loopCount = 0; str[loopCount] != '\0'; loopCount ++)
{
if(isalpha(str[loopCount]) != 0)
letterNumber ++;
else if(isdigit(str[loopCount]) != 0)
digitNumber ++;
else if(str[loopCount] == ' ')
blankNumber ++;
else
otherCharNumber ++;
}
}

cout << "There are " << letterNumber << " letters!" << endl
<< "There are " << digitNumber << " digits!" << endl
<< "There are " << blankNumber << " blanks!" << endl
<< "There are " << otherCharNumber << " other characters!" << endl;
return 0;
}
楼上那个好像不能读入空格的哦^_^

#include<iostream.h>
void main()
{
char ch;
int nother=0,ndigit[10];
for(int i=0;i<10;i++)
{
ndigit[i]=0;
}
cout<<"输入一些字符(以#结束)"<<endl;
cin>>ch;
while(ch!='#')
{
switch(ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': ++ndigit[ch-'0'];
break;
default: ++nother;
}
cin>>ch;
}
cout<<"所有的数字(依次为0~9各个数字出现的次数):";
for(int j=0;j<10;j++)
{
cout<<ndigit[j]<<' ';
}
cout<<"\n其他字符:"<<nother<<endl;
}