当前位置: 代码迷 >> C语言 >> 输入一篇文章,让你统计字符个数
  详细解决方案

输入一篇文章,让你统计字符个数

热度:271   发布时间:2006-03-26 10:43:00.0

#include <stdio.h>
#include <string.h>
main()
{
void str_count();
char str[100000];
int J;
gets(str);
J=strlen(str);
str_count(str,J);
}

void str_count(char str[],int J)
{
int T,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
for(T=0;T<J;T++)
if(str[T]=='a') a=a+1; /*jjj*/
else if(str[T]=='b')b=b+1;
else if(str[T]=='c')c=c+1;
else if(str[T]=='d')d=d+1;
else if(str[T]=='e')e=e+1;
else if(str[T]=='f')f=f+1;
else if(str[T]=='g')g=g+1;
else if(str[T]=='h')h=h+1;
else if(str[T]=='i')i=i+1;
else if(str[T]=='j')j=j+1;
else if(str[T]=='k')k=k+1;
else if(str[T]=='l')l=l+1;
else if(str[T]=='m')m=m+1;
else if(str[T]=='n')n=n+1;
else if(str[T]=='o')o=o+1;
else if(str[T]=='p')p=p+1;
else if(str[T]=='q')q=q+1;
else if(str[T]=='r')r=r+1;
else if(str[T]=='s')s=s+1;
else if(str[T]=='t')t=t+1;
else if(str[T]=='u')u=u+1;
else if(str[T]=='v')v=v+1;
else if(str[T]=='w')w=w+1;
else if(str[T]=='x')x=x+1;
else if(str[T]=='y')y=y+1;
else if(str[T]=='z')z=z+1;
printf("\na:%d\nb:%d\nc:%d\nd:%d\ne:%d\nf:%d\ng:%d\nh:%d\ni:%d\nj:%d\nk:%d\nl:%d\nm:%d\nn:%d\no:%d\np:%d\nq:%d\nr:%d\ns:%d\nt:%d\nu:%d\nv:%d\nw:%d\nx:%d\ny:%d\nz:%d",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z);

}
根据你的原程序,我改动了下,可以算出数组中每个字母出现的次数.


----------------解决方案--------------------------------------------------------

#include <stdio.h>
#include <string.h>
void main()
{
void str_count(char str[],int J);//////////
char str[100000];
int J;
gets(str);
J=strlen(str);
str_count(str,J);
}

void str_count(char str[],int J)
{
int T,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
for(T=0;T<J;T++)
if(str[T]=='a') a=a+1;
else if(str[T]=='b')b=b+1;
else if(str[T]=='c')c=c+1;
else if(str[T]=='d')d=d+1;
else if(str[T]=='e')e=e+1;
else if(str[T]=='f')f=f+1;
else if(str[T]=='g')g=g+1;
else if(str[T]=='h')h=h+1;
else if(str[T]=='i')i=i+1;
else if(str[T]=='j')j=j+1;
else if(str[T]=='k')k=k+1;
else if(str[T]=='l')l=l+1;
else if(str[T]=='m')m=m+1;
else if(str[T]=='n')n=n+1;
else if(str[T]=='o')o=o+1;
else if(str[T]=='p')p=p+1;
else if(str[T]=='q')q=q+1;
else if(str[T]=='r')r=r+1;
else if(str[T]=='s')s=s+1;
else if(str[T]=='t')t=t+1;
else if(str[T]=='u')u=u+1;
else if(str[T]=='v')v=v+1;
else if(str[T]=='w')w=w+1;
else if(str[T]=='x')x=x+1;
else if(str[T]=='y')y=y+1;
else if(str[T]=='z')z=z+1;
printf("\na:%d\nb:%d\nc:%d\nd:%d\ne:%d\nf:%d\ng:%d\nh:%d\ni:%d\nj:%d\nk:%d\nl:%d\nm:%d\nn:%d\no:%d\np:%d\nq:%d\nr:%d\ns:%d\nt:%d\nu:%d\nv:%d\nw:%d\nx:%d\ny:%d\nz:%d",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z);

}


----------------解决方案--------------------------------------------------------
这样子的程序不好,太多if语句

其实可以把if 换成switch 语句来做,程序比较清晰,可读性好多了
----------------解决方案--------------------------------------------------------
好呀,不过太长了.不精练的.
----------------解决方案--------------------------------------------------------
这个题目更本就不是让你用IF或是switch之类的.是让你用循环.晕


----------------解决方案--------------------------------------------------------
程序是这样的:
#include<stdio.h>
#include<string.h>
void main()
{char *p,c[1000];
int i,a[26];
printf("input the string");
fflush(stdin);
gets(c);
for(i=0;i<26;i++)a[i]=0;
p=c;
while(*p!=\'0')
{if(*p>='a'&&*p<='z')a[*p-'a']++;
if(*p>='A'&&*p<='Z')a[*p-'A']++;
p++;
}
for(i=0;i<26;i++)printf("%d\n",a[i]);
getch();
}

----------------解决方案--------------------------------------------------------


#include <stdio.h>

void main()
{
int sum[123]={0};
char c;

while((c=getchar())!='\n')sum[c]++;
for(c=0;c<=122;c++)if(sum[c]!=0)printf("the count of %c are %d\n",c,sum[c]);

}


----------------解决方案--------------------------------------------------------

#include <stdio.h>

void main()
{
int sum[123]={0};
char c;

while((c=getchar())!='\n')sum[c]++;
for(c=0;c<=122;c++)if(sum[c]!=0)printf("the count of %c are %d\n",c,sum[c]);

}


----------------解决方案--------------------------------------------------------
如果只差字母直接用个char alpha[26]就可以了,反正'a'到'z'是连续的。20行之内搞定。
----------------解决方案--------------------------------------------------------
哇,看不懂!
----------------解决方案--------------------------------------------------------
  相关解决方案