MacKey
MacKey

Reputation: 88

Comparing two vector strings for similarities C++

I'm using two string vectors to store two text files. I need to compare both and change the word to "*" of matched words. I have got all that working for strings that match 100% ('bat' to 'bat') but I need it to also include battle as it has the string 'bat' in it. I have tried to use strcmp but have had no luck! If anyone can be helpful and try and point me in the right direction. Thank you. The testlist vector contains all the word list and the inputlist contains the raw-data (sentences and words).

Here are the codes:

for (int j=0; j < testlist.size(); j++)
{
    for (int i = 0; i < inputlist.size(); i++)
    {
        if (inputlist[i] == testlist[j])
        {
            inputlist[i] ="*";
        }
    }
}

Upvotes: 1

Views: 7995

Answers (3)

Qaz
Qaz

Reputation: 61970

If you're looking to replace every string containing the term, or just the term with asterisks, for_each and string::find, along with string::replace are a good combination.

#include <iostream>
using std::cout;

#include <vector>
using std::vector;

#include <string>
using std::string;

#include <algorithm> //for_each

#define REPLACE_WORD

int main()
{
    vector<string> testlist (3); //your file
    testlist [0] = "bat";
    testlist [1] = "battle";
    testlist [2] = "Hello";

    string searchTerm = "bat";

    for_each (testlist.begin(), testlist.end(), //iterate through vector
        [&](string &word) {                     //calling this lambda for each
            #ifdef REPLACE_WORD //replacing whole word
                if (word.find (searchTerm) != string::npos) //if term is found
                    word.replace (0, word.length(), word.length(), '*'); //replace starting at char 0 for length() chars, with length() *s
            #else //REPLACE_TERM
                if (word.find (searchTerm) != string::npos)
                    word.replace (word.find (searchTerm), searchTerm.length(), searchTerm.length(), '*'); //same, but start at where it finds the term, and only replace that
            #endif
        } //end lambda
    ); //end for_each

    for_each (testlist.begin(), testlist.end(), [](string word){cout << word << ' ';}); //output vector
}

This outputs:
*** ****** Hello

Whereas changing REPLACE_WORD to REPLACE_TERM results in:
*** ***tle Hello

The lambda can be replaced with a normal function address if it suits you better.

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154045

It seems, all you need to do to match a word is to see if the word in the input list contains the word in the test list. You can detect containment using e.g. word.find(contains) != std::string::npos to see if word contains the string contains.

Upvotes: 1

twain249
twain249

Reputation: 5706

you can use find() instead of strcmp()

size_t found = inputlist[i].find(testlist[j]);
if(found != string::npos) {
   inputlist[i] = "****";
}

Upvotes: 2

Related Questions