输入:
"aabbAAABBCCCCZZ"
输出:
a -------2
A -------3
b -------2
B -------2
c -------0
C -------4...
Z -------2
实例分析:
1.对字符串用while循环或者for循环进行遍历,找到26个英文字母大小写;
2.将所有英文字母大写或者小写都转换成ASCII值,进行运算;
3.封装相应的功能函数,在main函数中进行函数调用即可;
示例代码:
#include<stdio.h>#include<string.h>voidinit_string(char*p){
scanf("%s",p);}voidcount_string(char*p,int*num1,int*num2){
int len =strlen(p);for(int i =0;i < len; i++){
if(p[i]>='a'&& p[i]<='z'){
num1[p[i]-'a']+=1;}if(p[i]>='A'&& p[i]<='Z'){
num2[p[i]-'A']+=1;}}}voidprint_string(int*num1,int*num2){
for(int i =0; i <26; i++){
printf("%c ------- %d\n",'a'+i, num1[i]);printf("%c ------- %d\n",'A'+i, num2[i]);}}intmain(int argc,charconst*argv[]){
char a[200]={
0};int m[26]={
0};int n[26]={
0};printf("请输入一段纯英文的字符串>>\n");init_string(a);count_string(a,m,n);print_string(m,n);return0;}
运行结果:
aabbAAABBCCCCZZ
a -------2
A -------3
b -------2
B -------2
c -------0
C -------4
d -------0
D -------0
e -------0
E -------0
f -------0
F -------0
g -------0
G -------0
h -------0
H -------0
i -------0
I -------0
j -------0
J -------0
k -------0
K -------0
l -------0
L -------0
m -------0
M -------0
n -------0
N -------0
o -------0
O -------0
p -------0
P -------0
q -------0
Q -------0
r -------0
R -------0
s -------0
S -------0
t -------0
T -------0
u -------0
U -------0
v -------0
V -------0
w -------0
W -------0
x -------0
X -------0
y -------0
Y -------0
z -------0
Z -------2