C语言 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
展开全部
#include<iostream.h>
#include<stdio.h>
typedef int KeyType;
#define MAXSIZE 26
typedef struct{
KeyType key;
KeyType keynumber;
}RecType;
typedef struct{
RecType r[MAXSIZE+1];
int length;
}SqList;
void Bubblesort(SqList &q) //冒泡排序
{
int j,p=q.length,temp1,temp2,flag=1,ps;
for(p;p>0&&flag==1;p=ps)
{
flag=0;
for(j=0;j<p;j++)
if(q.r[j].key>=q.r[j+1].key)
{
temp1=q.r[j].key;
temp2=q.r[j].keynumber;
q.r[j].key=q.r[j+1].key;
q.r[j].keynumber=q.r[j+1].keynumber;
q.r[j+1].key=temp1;
q.r[j+1].keynumber=temp2;
flag=1;
ps=j;
}
}
}
void main()
{
char ch;
int number=0,bigletter=0,smallletter=0,blank=0,other=0,num,line,i=0;
int schar[26],bchar[26];
float percent=0.0;
SqList s;
for(num=0;num<26;num++)
{
schar[num]=0;
bchar[num]=0;
}
cout<<"输入一串字符:"<<endl;
while((ch=getchar())!='@')
{
if(ch>='A'&&ch<='Z')
{
bigletter++;
for(num=0;num<26;num++)
{
if(ch==(num+65))
{
bchar[num]++;
}
}
}
else
if(ch>='a'&&ch<='z')
{
smallletter++;
for(num=0;num<26;num++)
{
if(ch==(num+97))
{
schar[num]++;
}
}
}
else
if(ch>='0'&&ch<='9')
number++;
else
if(ch==' ')
blank++;
else
other++;
}
cout<<"数字的个数:"<<number<<endl;
cout<<"大写字母的个数:"<<bigletter<<endl;
cout<<"小写字母的个数:"<<smallletter<<endl;
cout<<"字母的总个数:"<<bigletter+smallletter<<endl;
cout<<"空格的个数:"<<blank<<endl;
cout<<"其它字符的个数:"<<other<<endl<<endl;
cout<<"各个字母统计:"<<endl;
cout<<"小写字母:"<<endl;
line=0;
for(num=0;num<26;num++)
{
percent=percent+schar[num];
}
for(num=0;num<26;num++)
if(schar[num]>0)
{
i++;
s.length=i;
s.r[i].key=schar[num];
s.r[i].keynumber=num;
}
Bubblesort(s);
for(num=i;num>=1;num--)
{
printf("%c=%5d,%6.2f%% ",s.r[num].keynumber+97,s.r[num].key,s.r[num].key/percent*100);
}
cout<<endl;
cout<<"大写字母:"<<endl;
line=0;
for(num=0;num<26;num++)
if(bchar[num]>0)
{
line++;
printf("%c:%d ",num+65,bchar[num]);
if(line%10==0)
cout<<endl;
}
cout<<endl<<endl;
}
本程序以"@"字符结束输入,因此不能统计"@"字符和以后的字符,可以修改while((ch=getchar())!='@')语句来修改结束输入符!
#include<stdio.h>
typedef int KeyType;
#define MAXSIZE 26
typedef struct{
KeyType key;
KeyType keynumber;
}RecType;
typedef struct{
RecType r[MAXSIZE+1];
int length;
}SqList;
void Bubblesort(SqList &q) //冒泡排序
{
int j,p=q.length,temp1,temp2,flag=1,ps;
for(p;p>0&&flag==1;p=ps)
{
flag=0;
for(j=0;j<p;j++)
if(q.r[j].key>=q.r[j+1].key)
{
temp1=q.r[j].key;
temp2=q.r[j].keynumber;
q.r[j].key=q.r[j+1].key;
q.r[j].keynumber=q.r[j+1].keynumber;
q.r[j+1].key=temp1;
q.r[j+1].keynumber=temp2;
flag=1;
ps=j;
}
}
}
void main()
{
char ch;
int number=0,bigletter=0,smallletter=0,blank=0,other=0,num,line,i=0;
int schar[26],bchar[26];
float percent=0.0;
SqList s;
for(num=0;num<26;num++)
{
schar[num]=0;
bchar[num]=0;
}
cout<<"输入一串字符:"<<endl;
while((ch=getchar())!='@')
{
if(ch>='A'&&ch<='Z')
{
bigletter++;
for(num=0;num<26;num++)
{
if(ch==(num+65))
{
bchar[num]++;
}
}
}
else
if(ch>='a'&&ch<='z')
{
smallletter++;
for(num=0;num<26;num++)
{
if(ch==(num+97))
{
schar[num]++;
}
}
}
else
if(ch>='0'&&ch<='9')
number++;
else
if(ch==' ')
blank++;
else
other++;
}
cout<<"数字的个数:"<<number<<endl;
cout<<"大写字母的个数:"<<bigletter<<endl;
cout<<"小写字母的个数:"<<smallletter<<endl;
cout<<"字母的总个数:"<<bigletter+smallletter<<endl;
cout<<"空格的个数:"<<blank<<endl;
cout<<"其它字符的个数:"<<other<<endl<<endl;
cout<<"各个字母统计:"<<endl;
cout<<"小写字母:"<<endl;
line=0;
for(num=0;num<26;num++)
{
percent=percent+schar[num];
}
for(num=0;num<26;num++)
if(schar[num]>0)
{
i++;
s.length=i;
s.r[i].key=schar[num];
s.r[i].keynumber=num;
}
Bubblesort(s);
for(num=i;num>=1;num--)
{
printf("%c=%5d,%6.2f%% ",s.r[num].keynumber+97,s.r[num].key,s.r[num].key/percent*100);
}
cout<<endl;
cout<<"大写字母:"<<endl;
line=0;
for(num=0;num<26;num++)
if(bchar[num]>0)
{
line++;
printf("%c:%d ",num+65,bchar[num]);
if(line%10==0)
cout<<endl;
}
cout<<endl<<endl;
}
本程序以"@"字符结束输入,因此不能统计"@"字符和以后的字符,可以修改while((ch=getchar())!='@')语句来修改结束输入符!
展开全部
#include<stdio.h>
#include<string.h>
main()
{char c[100];
int i,n,digital=0,blank=0,other=0,ch=0;
printf("please input the string:\n");
gets(c);
n=strlen(c);
for(i=0;i<n;i++)
{if((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z'))
ch++;
else if(c[i]=' ')blank++;
else if(c[i]>=0&&c[i]<=9)digital++;
else other++;}
printf{"%d%d%d%d",digital,blank,ch,other);
}
#include<string.h>
main()
{char c[100];
int i,n,digital=0,blank=0,other=0,ch=0;
printf("please input the string:\n");
gets(c);
n=strlen(c);
for(i=0;i<n;i++)
{if((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z'))
ch++;
else if(c[i]=' ')blank++;
else if(c[i]>=0&&c[i]<=9)digital++;
else other++;}
printf{"%d%d%d%d",digital,blank,ch,other);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询