用C++指针编写输入一行文字,找出其中大小写字母,空格,数字及其它字符各有多少?

#include<iostream>
using namespace std;
int main()
{
int i,A=0,a=0,space=0,d=0,other=0;
char *p,s[20];
while((s[i]=getchar())!='\n')i++;
p=s;
while(*p='\n')
{
if(('A'<=*p)&&(*p<='Z'))A++;
else
if(('a'<=*p)&&(*p<='a'))a++;
else
if(('0'<=*p)&&(*p<='9'))d++;
else
if(*p==' ')space++;
else
other++;
p++;
}
cout<<"大写字母:"<<A<<"小写字母:"<<a<<"数字:"<<d<<"空格:"<<space<<"其它符号:"<<other<<endl;
return 0;
}
总是说有错误
为什么错了?

#include<iostream>
using namespace std;
int main()
{
//i初始化
int i=0,A=0,a=0,space=0,d=0,other=0;
char *p,s[20];
while((s[i]=getchar())!='\n')i++;
// gets(s);//这话用得干脆
p=s;
while(*p!='\n')//不等于是这么判断的
{
if(('A'<=*p)&&(*p<='Z'))A++;
else
if(('a'<=*p)&&(*p<='a'))a++;
else
if(('0'<=*p)&&(*p<='9'))d++;
else
if(*p==' ')space++;
else
other++;
p++;
}
cout<<"大写字母:"<<A<<"小写字母:"<<a<<"数字:"<<d<<"空格:"<<space<<"其它符号:"<<other<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-02
#include<iostream>
using namespace std;
int main()
{
int i=0,A=0,a=0,space=0,number=0,other=0;
char *p,s[100];
cout<<"请输入一行文字:"<<endl;
cin.getline(s,100);
p=s;
while(*p!='\0')
{
if(('A'<=*p)&&(*p<='Z'))A++;
else
if(('a'<=*p)&&(*p<='z'))a++;
else
if(('0'<=*p)&&(*p<='9'))number++;
else
if(*p==' ')space++;
else
other++;
p++;
}
cout<<"大写字母:"<<A<<"小写字母:"<<a<<"数字:"<<number<<"空格:"<<space<<"其它符号:"<<other<<endl;

return 0;
}
第2个回答  2012-03-04
//以下修改完了:错的地方比较多!
#include<iostream>
using namespace std;
int main()
{
int i=0,A=0,a=0,space=0,d=0,other=0;//(1)你的i没有初始化
char *p,s[20];
memset(s,0,20);//(2)你的S没有初始化
while((s[i]=getchar())!='\n')i++;
p=s;
while(*p)//循环判断
{
if(('A'<=*p)&&(*p<='Z'))A++;
else
if(('a'<=*p)&&(*p<='z'))a++;//笔误z写成了a
else
if(('0'<=*p)&&(*p<='9'))d++;
else
if(*p==' ')space++;
else
other++;
p++;
}
cout<<"大写字母:"<<A<<"\n小写字母:"<<a<<"\n数字:"<<d<<"\n空格:"<<space<<"\n其它符号:"<<other<<endl;
return 0;
}
第3个回答  2012-03-04
你的getchar少个头文件
至于你的程序的输出结果是否正确我就没看了
加个#include "stdio.h"
就可以编译运行了
第4个回答  2012-03-04
早忘记了
相似回答