Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,276 questions

52,302 answers

573 users

How to group words in a string by the first N letters in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <regex>
#include <unordered_map>

std::unordered_map<std::string, std::vector<std::string>>
group_by_first_n_letters(const std::string& s, std::size_t n = 3)
{
    std::unordered_map<std::string, std::vector<std::string>> groups;

    std::regex word_regex("[A-Za-z]+");
    auto words_begin = std::sregex_iterator(s.begin(), s.end(), word_regex);
    auto words_end   = std::sregex_iterator();

    for (auto it = words_begin; it != words_end; ++it) {
        std::string w = it->str();
        std::transform(w.begin(), w.end(), w.begin(), ::tolower);

        if (w.size() >= n) {
            groups[w.substr(0, n)].push_back(w);
        }
    }

    return groups;
}

int main() {
    std::string s = "The lowly inhabitants of the lowland were surprised to see "
                    "the lower branches of the trees.";

    auto groups = group_by_first_n_letters(s, 3);

    for (const auto& [prefix, words] : groups) {
        std::cout << prefix << ": ";
        for (const auto& w : words) std::cout << w << " ";
        std::cout << "\n";
    }
}



/*
run:

bra: branches 
see: see 
sur: surprised 
tre: trees 
wer: were 
inh: inhabitants 
low: lowly lowland lower 
the: the the the the 

*/

 



answered 1 day ago by avibootz
0 votes
#include <iostream>
#include <string>
#include <regex>
#include <unordered_map>
#include <vector>

std::unordered_map<std::string, std::vector<std::string_view>>
group_by_first_n_letters(std::string& s, std::size_t n = 3)
{
    std::unordered_map<std::string, std::vector<std::string_view>> groups;

    std::regex word_regex("[A-Za-z]+");
    auto begin = std::sregex_iterator(s.begin(), s.end(), word_regex);
    auto end   = std::sregex_iterator();

    for (auto it = begin; it != end; ++it) {
        std::string_view w(&s[it->position()], it->length());

        // lowercase in-place on the original string
        for (char& c : s.substr(it->position(), it->length()))
            c = std::tolower(c);

        if (w.size() >= n) {
            groups[std::string(w.substr(0, n))].push_back(w);
        }
    }

    return groups;
}


int main() {
    std::string s = "The Lowly inhabitants of the lowland were surprised to see "
                    "the lower branches of the trees.";

    auto groups = group_by_first_n_letters(s, 3);

    for (const auto& [prefix, words] : groups) {
        std::cout << prefix << ": ";
        for (const auto& w : words) std::cout << w << " ";
        std::cout << "\n";
    }
}



/*
run:

bra: branches 
see: see 
sur: surprised 
tre: trees 
wer: were 
low: lowland lower 
the: the the the 
inh: inhabitants 
Low: Lowly 
The: The 

*/

 



answered 1 day ago by avibootz
...