算法 字符个数统计

题目描述

编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127),换行表示结束符,不算在字符里。不在范围内的不作统计。

输入描述:

输入N个字符,字符在ACSII码范围内。

输出描述:

输出范围在(0~127)字符的个数。

示例1

输入

复制
abc

输出

复制
3

直接hash:

#include <iostream>
#include <string>
using namespace std;


int main() {

  string str;
  char sz[128]={0};
  int length = 0;
  int count=0;
  cin>>str;
  length = str.size();

  for(int i = 0; i < length; i++) {
    sz[str[i]] = 1;
  }
  for(int i = 0; i < 128;i++){
    if(sz[i]!=0){
      count++;
    }
  }

  cout << count << endl;
  return 0;
}

原文地址:https://www.cnblogs.com/liuruoqian/p/11644851.html