Laina
Laina

Reputation: 29

Give list of string value

std::string text;
std::getline(std::cin, text);

With the above as the set up, how would I identify a list of strings, that would be inputed in text, equal a single value?

EX:

std::string text;
std::getline(std::cin, text);
std::string aux; //Added
text.find("word ", "thisword", "another", "floor") = aux; //Added

if(text.find("lutece labs" + aux) != std::string::npos); //Goal
{
...megh...
}

I feel like I butchered the above code, but I hope it explains what I'm looking for. All string input will be from text. So how could I make a list of words that would be found in text, so I can make the new list equal a single value? Hope I asked it clearly. Thank you!

Upvotes: 0

Views: 132

Answers (2)

David G
David G

Reputation: 96810

Try the following:

#include <algorithm>
#include <string>
#include <iostream>

namespace detail
{
    template<int... Is>
    struct index_sequence { };

    template<int N, int... Is>
    struct make_index_sequence : make_index_sequence<N-1, N-1, Is...> { };

    template<int... Is>
    struct make_index_sequence<0, Is...> : index_sequence<Is...> { };

    void replace_all_with_t(std::string, std::string)
    {
    }

    template<class... Tail>
    void replace_all_with_t(std::string& c,
                            std::string value,
                            std::string head, Tail&&...tail)
    {
        while (c.find(head) != std::string::npos)
            c.replace(c.find(head), head.size(), value);

        replace_all_with_t(c, std::move(value), std::forward<Tail>(tail)...);
    }

    template<class Tuple, int... Is>
    void replace_all_with(std::string& c,
                          Tuple&& tokens,
                          std::string value,
                          detail::index_sequence<Is...>)
    {
        replace_all_with_t(c, std::move(value), std::get<Is>(std::forward<Tuple>(tokens))...);
    }
}

template<class Tuple>
void replace_all_with(std::string& c,
                      Tuple&& tokens,
                      std::string value)
{
    detail::replace_all_with(c, std::forward<Tuple>(tokens), std::move(value),
               detail::make_index_sequence<std::tuple_size<Tuple>::value>());
}

int main()
{
    std::string s = "abchellojsoncrazyabcworldabc";
    replace_all_with(s, std::make_tuple("abc", "json"), "*");
    std::cout << s;
}

Live Demo

Upvotes: 0

P0W
P0W

Reputation: 47794

You can have something like following, may not be the best approach :

#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>
#include <iostream>

int main()
{
        std::string text = "This is a long text word \
                            this word another floor king \
                            queen ace";

    std::stringstream ss(text) ; 

    std::vector<std::string> vec{ // Form a vector of string
                std::istream_iterator<std::string>(ss),
                std::istream_iterator<std::string>() };

    // Get list of words to be searched for
    std::vector<std::string> to_find {"word ", "this", 
                          "word", "another", "floor"};                   

    std::string aux ="jack"; // the replace word

    std::replace_if(   vec.begin( ),vec.end( ), /* Loop over vec */
                    [to_find](const std::string& x)
                    {  /* Find if any from to_find is present */
                       return std::any_of( 
                              to_find.begin(), 
                              to_find.end(),
                              [x](const std::string& y)
                              { return x == y; }
                                         ) ;
                    },
                    aux );

    /* Now get your modified text */                
    text =std::accumulate( vec.begin()+1, vec.end( ), 
                           vec[0],
                         [](std::string s0, std::string const& s1) 
                         { return s0 += " " + s1; }
                        );
    std::cout << text ;

}

See Here (just a simple demo, you need to check for boundary conditions)

Upvotes: 1

Related Questions