Developer
Developer

Reputation: 11

How to convert a string to a dictionary of letters?

I need to convert letters into a dictionary of characters. Here's an example:

letter

l: 1
e: 2
t: 2
r: 1

I did some research and found this helpful answer, but that was using getline() and separating words by spaces. Since I am trying to split by character I don't think I can use getline() since '' isn't a valid split character. I could convert to a char* array but I wasn't sure where that would get me.

This is fairly easy in other languages so I thought it wouldn't be too bad in C++. I was hoping there would be something like a my_map[key]++ or something. In Go I would write this as

// Word map of string: int values
var wordMap = make(map[string]int)

// For each letter, add to that key
for i := 0; i < len(word); i++ {
    wordMap[string(word[i])]++
}

// In the end you have a map of each letter.

How could I apply this in C++?

Upvotes: 1

Views: 881

Answers (3)

user12002570
user12002570

Reputation: 1

I wrote an article about this which can be checked out here. Below i have given 2 versions of the program. Version 1 keeps track of the character count in alphabetical order. But sometimes(in case) you want the character count in insertion order for which you can use Version 2.

Version 1: Get character count in ͟a͟l͟p͟h͟a͟b͟e͟t͟i͟c͟a͟l͟ ͟o͟r͟d͟e͟r͟

#include <iostream> //needed for std::cout, std::cin
#include <map>      //needed for std::map
#include <iomanip>  //needed for formating the output (std::setw)

int main() 
{
    std::string inputString; //user input will be read into this string variable
    std::cout << "Enter a string: " << std::endl;
    std::getline(std::cin, inputString);
    //this map maps the char to their respective count
    std::map < char, int > charCount;
    //iterate through the inputString
    for (char & c: inputString) 
    {
        charCount[c]++;//increment the count for character c
    }
    std::cout << "Total unique characters are: " << charCount.size() << std::endl;
    std::cout << "------------------------------------" << std::endl;
    std::cout << "Character" << std::setw(10) << "Count" << std::endl;
    std::cout << "------------------------------------" << std::endl;
    for (std::pair < char, int > pairElement: charCount) 
    {
        std::cout << std::setw(4) << pairElement.first << std::setw(13) << pairElement.second << std::endl;
    }
    return 0;
}

Version 2: Get character count in i͟n͟s͟e͟r͟t͟i͟o͟n͟ ͟o͟r͟d͟e͟r͟

#include <iostream> 
#include <map>      
#include <iomanip>  

int main() 
{
    std::string inputString; 
    std::cout << "Enter a string: " << std::endl;
    std::getline(std::cin, inputString);
    
    std::map < char, int > charCount;
    
    for (char & c: inputString) 
    {
        charCount[c]++;
    }
    std::cout << "Total unique characters are: " << charCount.size() << std::endl;
    std::cout << "------------------------------------" << std::endl;
    std::cout << "Character" << std::setw(10) << "Count" << std::endl;
    std::cout << "------------------------------------" << std::endl;
    std::size_t i = 0;
    //just go through the inputString instead of map
    for(char &c: inputString)
    {
        std::size_t index = inputString.find(c);
        if(index != inputString.npos && (index == i)){
         std::cout << std::setw(4) << c << std::setw(13) << charCount.at(c)<<std::endl;
         
        }
        ++i;
    }
    return 0;
}

Upvotes: -1

floomby
floomby

Reputation: 301

Here is the unicode version of Drew Dormann's answer:

#include <locale>
#include <codecvt>

std::string word = "some unicode: こんにちは世界";

std::map<char32_t, uint> wordMap;
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;

for (auto c : converter.from_bytes(word)) {
    wordMap[c]++;
}

for (const auto [c, v] : wordMap) {
    std::cout << converter.to_bytes(c) << " : " << v << std::endl;
}

Upvotes: 1

Drew Dormann
Drew Dormann

Reputation: 63775

How could I apply this in C++?

It could look rather similar to your Go code.

// Word map of char: int values
// (strings would be overkill, since you know they are a single character)
auto wordMap = std::map<char,int>{};

// For each letter, add to that key
for ( char c : word )
    wordMap[c]++;
}

Upvotes: 1

Related Questions