标记,利用一个数组标记,标记原来数组的数据,较快运行,需要对原来数组操作时,可对标记数组进行调用。
问题:
找出一行字符中各个字母的个数,并按顺序输出
代码:
#include<stdio.h>
#include<string.h>
int main()
{
char q[100001],m;
int i,n,j,a[123];
while(gets(q))
{
int a[123]={0};
j=strlen(q);
for(i=0;i<=j-1;i++)
{
if(q[i]>='a'&&q[i]<='z')
a[q[i]]++;
}
for(m='a';m<='z';m++)
{
printf("%c:%d\n",m,a[m]);
}
printf("\n");
}
}
输出样例:work
a:0
b:0
c:0
d:0
e:0
f:0
g:0
h:0
i:0
j:0
k:1
l:0
m:0
n:0
o:1
p:0
q:0
r:1
s:0
t:0
u:0
v:0
w:1
x:0
y:0
z:0