HUAWEI Written Questions: Statistics of Characters

Title description

Write a function to count the number of different characters contained in the string. The character is within the range of ACSII code (0 ~ 127), and the newline indicates the end character, not included in the character. Those not within the scope are not counted.

Enter description:

Enter N characters, the characters are within the range of ACSII code.

Output description:

The output range is (0 ~ 127) characters.

Example 1

Input

abc

Output

3
#include <iostream>
#include <string>

using namespace std;

int main() {
    string s;
    getline(cin ,s);
    int ar[128] = {0};
    for (int i = 0; i < s.length(); ++i) {
        ar[int(s[i])]++;
    }
    int num = 0;
    for (int j = 0; j < 128; ++j) {
        if(ar[j]) num++;
    }
    cout << num << endl;
    return 0;
}

 

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/104789290