统计字符出现的次数和数字出现的次数



#include<iostream>
#include<cstdlib>
//下面这段代码不可少, 
using namespace std;
//统计ASCII码出现的次数 
void histogram(char* src){
	int i;
	char hist[256];
	for(i=0;i<=255;i++){
		hist[i]=0;
		//cout<<hist[i];
	}
	cout<<src;
	cout<<endl; 
	//循环读取字符串中的字符 ,直到字符串结尾 
	while(*src!='\0'){		
		hist[*src]++;
		//向后移动 
		*src++;		
	}
	
	for(i=0;i<=255;i++){
		printf("%d  ",hist[i]);
	}
	cout<<endl;
}
//统计a-z和A-Z出现的次数,统计数字出现的次数 
void histoaz(char* src){
	int i=0;
	char hist[26];
	int  m=0;
	for(i=0;i<26;i++){
		hist[i]=0;
	}
	
	while(*src!='\0'){
		if(*src>='a' && *src<='z'){
			int n=*src-'a';
			hist[n]++;
		}else
		if(*src>='A' && *src<='Z'){
			int n=*src-'A';
			hist[n]++;
		}else if(*src>='0'&&*src<='9'){
			//记录出现数字的次数 
			m++;
		} 
		*src++;
	}
	//循环输出字母出现的次数 
	for(i=0;i<26;i++){
		printf("%c %d  ",(i+65),hist[i]);
	}
	cout<<endl;
	cout<<m<<endl;
} 



int main(){
	
	char *src="dsfasdfs1354532mmnbjhkjoopsdadf2132165489798";
	histogram(src);
	histoaz(src);
	return  0;
}

输出结果::








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值