输入一行字符,分别统计出其中大小写英文字母、空格、数字和其他字符的个数

#include<iostream>
#include<string>
#include "stdio.h"
using namespace std;
int main()
{int i,n;
cout<<"输入字符长度n=";
cin>>n;
char *p=new char[n];
cout<<"输入字符"<<endl;
for(i=0;i<n;i++)
cin.getline(p,n);
int a,b,c,d,e;a=b=c=d=e=0;
for(i=0;i<n;i++)
{if((p[i]>='A')&&(p[i]<='Z'))a++;
else if((p[i]>='a')&&(p[i]<='z'))b++;
else if((p[i]>='0')&&(p[i]<='9'))c++;
else if(p[i]==' ')d++;
else e++;}
cout<<"大写字母有"<<a<<"个"<<endl;
cout<<"小写字母有"<<b<<"个"<<endl;
cout<<"数字有"<<c<<"个"<<endl;
cout<<"空格有"<<d<<"个"<<endl;
cout<<"其他字符有"<<e<<"个"<<endl;
return 0;
}这个程序哪里错了<!--

#include<stdio.h>

int main()

{

char c;

int letters=0,spaces=0,digits=0,others=0;

printf("请输入一串任意的字符:\n");

while((c=getchar())!='\n')

{

if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))

letters++;

else if(c>='0'&&c<='9')

digits++;

else if(c==' ')

spaces++;

else

others++;

}

printf("字母有%d个,数字有%d个,空格有%d个,其他有%d个",letters,digits,spaces,others);

return 0;

}

扩展资料:

字符是可使用多种不同字符方案或代码页来表示的抽象实体。例如,Unicode UTF-16 编码将字符表示为16位整数序列,而 Unicode UTF-8 编码则将相同的字符表示为 8 位字节序列。微软的公共语言运行库使用 Unicode UTF-16(Unicode 转换格式,16 位编码形式)表示字符。

参考资料来源:百度百科-字符

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-24

#include<stdio.h>

int main()

{

char c;

int letters=0,spaces=0,digits=0,others=0;

printf("请输入一串任意的字符:\n");

while((c=getchar())!='\n')

{

if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))

letters++;

else if(c>='0'&&c<='9')

digits++;

else if(c==' ')

spaces++;

else

others++;

}

printf("字母有%d个,数字有%d个,空格有%d个,其他有%d个",letters,digits,spaces,others);

return 0;

}

扩展资料:

while语句若一直满足条件,则会不断的重复下去。但有时,需要停止循环,则可以用下面的三种方式:

一、在while语句中设定条件语句,条件不满足,则循环自动停止。

如:只输出3的倍数的循环;可以设置范围为:0到20。

二、在循环结构中加入流程控制语句,可以使用户退出循环。

1、break流程控制:强制中断该运行区内的语句,跳出该运行区,继续运行区域外的语句。

2、continue流程控制:也是中断循环内的运行操作,并且从头开始运行。

三、利用标识来控制while语句的结束时间。

参考资料来源:

百度百科——while

第2个回答  推荐于2017-10-06

你的程序没改时的错误,由此可见,你的程序p[0]  p[n] 第一个和最后一个字符不能识别

本回答被提问者采纳
第3个回答  2011-11-06
开始----输入-----提取第一个字符(计算输入的字符长度)-----比较是什么(如果是大写字母,a计数器+1;如果是小写字母,b计数器+1,....) -------提取第二个字符-----比较是什么(如果是大写字母,a计数器+1;如果是小写字母,b计数器+1,....)-----提取第三个字符-----比较是什么(如果是大写字母,a计数器+1;如果是小写字母,b计数器+1,....) 就这样下去
第4个回答  2014-11-15
以下程序在win-tc下调试通过 /* 输入一行文字 找出其中大写字母小写字母空格数字及其他字符各有多少 */ # include "stdio.h" # include "conio.h" void main(void) { int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[80]; printf("\nInput a string:"); while ((s[i]=getchar())!='\n') i++; p=s; while(*p!='\n') {if((*p>='A')&&(*p<='Z')) upper++; else if((*p>='a')&&(*p<='z')) lower++; else if(*p==' '||*p==9) space++; else if((*p>='0')&&(*p<='9')) digit++; else other++; p++; } printf("upper case:%d lower case:%d ",upper,lower); printf("space:%d digit:%d other:%d ",space,digit,other); getch(); }
满意请采纳。追问

我是用C++写的,怎么改

本回答被网友采纳
相似回答