#include<stdio.h>
#include<string.h>
int main()
{
void numchar(char *s);
char s[100];
printf("please enter a string:\n");
scanf("%s",s);
numchar(s);
return 0;
}
void numchar(char *s)
{
int i;
int count[256];
for(i=0; i<256; i++)
count[i] = 0;
//初始化,默认所有元素均为整形,且其初始值为0(刚开始统计时各个字符的个数均为0)
for(i=0; s[i] != '\0'; i++)
count[s[i]]++;
//s[i]作为下标时就是其对应字符的ascll码值,其范围为0-255,其实这里就已统计好
for(i=0; i<256; i++)
{
if(count[i]) //if(count[i] != 0)
printf("%c:%d\n",i,count[i]); //i做了强制类型转换
}
}
统计字符串中各个字符出现的次数
最新推荐文章于 2024-01-18 14:27:01 发布