简体   繁体   中英

How to calculate number of different number of characters in a string data in c++?

I am able to count the number of characters in my string using length() function. But i want to calculate the number of different characters in my string. ie say string is "Hello world" So here number of different strings are H,e,l,o, ,w,r,d. so 8 different characters.

The best way to do is a method called frequency checking. Basically create a vector of size 128. Go through the string and for every character, increment the frequency that matches its ASCII value. Finally, iterate over the freq vector and count how many non zero entries you have. Code should look like this:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
string s = "Hello World";
vector<int>freq(128);

for(int i = 0; i < s.length(); i++)
    freq[s[i]]++;

int counter = 0;
for(int i = 0; i < 128; i++)
    if(freq[i] > 0)
        counter++;

cout << counter << "\n";
}

Vector of size 128 works fine because ASCII codes only go from 0 to 127.

Another way is to initialize a std::set and insert every character of the string into that one at a time. Finally, output the size of the set. This works because set doesn't allow duplicate entries. The code for this looks like:

#include<iostream>
#include<set>
#include<string>

using namespace std;

int main()
{
    string s = "Hello World";
    set<char>x;
    for(int i = 0; i < s.length(); i++)
        x.insert(s[i]);

    cout << x.size() << "\n";
}

To count the number of unique characters, you can use sd::sort followed by std::unique . It will reshuffle the contents and return an iterator to the last unique character in your string. Subtract begin() and you have the result.

I think that an unordered_map is the best way to achieve that.

Here is the code if you want the total number of char in a string, grouped by unique chars.

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) { //for each char in string
        map[c]++;           
    }
    for (auto &e: map)  //for each unique char in map
            cout<<"char: "<<e.first<<" number: "<<e.second<<endl;

return 0;

    }

Output

char: n number: 1
char: e number: 1
char: i number: 1
char: t number: 3
char: s number: 2
char:   number: 1
char: g number: 1
char: r number: 1

But if you want only the total number of unique chars

#include <iostream>
#include <unordered_map>

using namespace std;
int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) {
        map[c]++;
    }
    int count =0;
    for (auto &e: map)
       count++;

    cout<<"Unique chars: "<<count<<endl;

}

Output

Unique chars: 8

Well you can write your own functions to deal with that like this

#include <iostream>

using namespace std;

string uniqueChars(string str) {
  string newStr = "";
  bool arr[128];
  for(int i = 0;i < 128; i++) {
    arr[i] = false;
  }
  char c;
  for(int i = 0, n = str.length();i < n; i++) {
    c = str[i];
    if(c < 0 || c > 127) {
      continue;
    }
    if(!arr[c]) {
      arr[c] = true;
      newStr += c;
    }
  }
  return newStr;
}

int main(void) {
  string a = "Hello It's a wonderful world";
  string b = uniqueChars(a);
  cout << a << " => " << a.length() << "\n" <<
    b << " => " << b.length();
  return 0;
}

Output:

Hello It's a wonderful world => 28
Helo It'sawndrfu => 16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM