我写的:避免双重循环:
#include "stdio.h"
#define N 100
int main() {
char s[N];
printf("Input a string:\n");
scanf("%s",s);
int i,up[26]={0},down[26]={0};
for(i=0;i
up[ s[i]-'A' ]++;
else if(s[i]>='a' && s[i]<='z') //小写字母
down[ s[i]-'a' ]++;
else { // 其它 出错
printf("What you input is not a valid string,error--> %c\n",s[i]);
return 0;
}
}
printf("The result is as follows:\n");
for(i=0;i<26;i++) {
if(up[i]!=0)
printf("%c----%d\n",i+'A',up[i]);
if(down[i]!=0)
printf("%c----%d\n",i+'a',down[i]);
}
return 0;
}
/*这个是统计给定的字符串,如果想统计人为输入的要加点语句,你自己看吧*/
main()
{char arr[10]={'c','c','d','a','r','k','n','e','s','s'};
int i,j,sum[26];
for(i=0;i<=26;i++)
sum[i]=1; /*先初值1*/
for(i=0;i<=10;i++)
for(j=i+1;j<=9;j++)
if (arr[i]==arr[j])
sum[i]=sum[i]+1; /*判断是否有相同的字符*/
for(i=0;i<=9;i++)
printf("%c:%d\n",arr[i],sum[i]);/*打印结果*/
}
我写的:避免双重循环:
#include
"stdio.h"
#define
N
100
int
main()
{
char
s[N];
printf("Input
a
string:\n");
scanf("%s",s);
int
i,up[26]={0},down[26]={0};
for(i=0;i
s[i]!=0;i++)
{
if(s[i]>='A'
&&
s[i]<='Z')
//
大写字母
up[
s[i]-'A'
]++;
else
if(s[i]>='a'
&&
s[i]<='z')
//小写字母
down[
s[i]-'a'
]++;
else
{
//
其它
出错
printf("What
you
input
is
not
a
valid
string,error-->
%c\n",s[i]);
return
0;
}
}
printf("The
result
is
as
follows:\n");
for(i=0;i<26;i++)
{
if(up[i]!=0)
printf("%c----%d\n",i+'A',up[i]);
if(down[i]!=0)
printf("%c----%d\n",i+'a',down[i]);
}
return
0;
}