字符个数统计——来自华为OJ平台测试基础篇



/*
 * 字符个数统计       
 * 描述:  编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
 * 知识点:  字符串,函数,指针 
 * 题目来源:  内部整理 
 * 练习阶段:  初级 
 * 运行时间限制: 10Sec
 * 内存限制: 128MByte
 * 输入: 
 * 输入N个字符,字符在ACSII码范围内(0~127)。
 * 输出: 
 * 输出字符的个数。
 * 样例输入: abc        
 * 样例输出: 3
*/
#include <iostream>
#include <map>
#include <string>

using namespace std;

void Input(std::string &str,int &strlen);
void Count(std::string str,int strlen,int &number);

int main()
{
 std::string str;
 int strlen = 0;
 int num = 0;

 Input(str,strlen);
 Count(str,strlen,num);

 std::cout << num << endl;

 system("pause");

 return 0;
}

void Input(std::string &str,int &strlen)
{
 getline(std::cin,str,'\n');
 strlen = str.size();

 return;
}

void Count(std::string str,int strlen,int &number)
{
 typedef map<char,int> celements;
 char c;
 celements paris;

 for(int i=0;i<strlen;i++)
 {
  c = str.at(i);

  if((c>=0) && (c<=127))
  {
   ++paris[c];
  } 
 }

 number = paris.size();

 return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值