华为机试-字符个数统计

题目描述

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

输入描述:

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

输出描述:

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

示例1

输入

abc

输出

3

我的解答:

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

int main()
{
 int a[129]={0};
 int count=0;
 int m;
 string str;
 getline(cin,str);
 m=str.size();
 for(int i=0;i<m;i++)
 {
  if(a[str[i]]==0)
  {
   count++;
   a[str[i]]++;
  }
 }
 cout<<count<<endl;
 return 0;
}

posted @ 2018-04-08 15:17  二十划生  阅读(133)  评论(0编辑  收藏  举报