//输入一行文字,找出其中大写字母、小写字母、空格、数字、及其他字符各有多少。
//嫌麻烦,我没有定义函数,也没有用指针,这个程序错哪了??
int main(int argc, char * argv[])
{
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
char s[20];
cout<<"Input string:";
for( i=0;i
{
cin>>s[i];
}
for(int i=0;s[i]!='\0';i++)
{
if((s[i]>='A')&&(s[i]<='Z'))
upper++;
else if((s[i]>='a')&&(s[i]<='z'))
lower++;
else if(s[i]==' ')
space++;
else if((s[i]>='0')&&(s[i]<='9'))
digit++;
else
other++;
}
cout<<upper <<'\t'<<lower<<'\t'<<space<<'\t'<<digit<<'\t' <<other;
return 0;
}
C++ 字符计算时遇到的问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
小灸舞 2016-05-02 05:48关注输入的地方有问题,直接改成cin.getline(s, 20);就行,用cin>>s是不行的,因为你需要读入含空格的字符串,cin是做不到的
#include<iostream> using namespace std; int main(int argc, char * argv[]) { int upper=0,lower=0,digit=0,space=0,other=0,i=0; char s[20]; cout<<"Input string:"; cin.getline(s, 20); for(int i=0;s[i]!='\0';i++) { if((s[i]>='A')&&(s[i]<='Z')) upper++; else if((s[i]>='a')&&(s[i]<='z')) lower++; else if(s[i]==' ') space++; else if((s[i]>='0')&&(s[i]<='9')) digit++; else other++; } cout<<upper <<'\t'<<lower<<'\t'<<space<<'\t'<<digit<<'\t' <<other; return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报