Strings and vectors, deleting duplicates of a word in a sentence.

So I have to make a code checks if a sentence is read the same forwards as backwards. Then make sure there are no duplicates of the same word used twice, ex - "I ate the the pizza."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector <string> words; 

void splitSent (string sent);
void checkSent ();
void findDups ();

int main ()
{
	string sent;
	
	cout << "Enter your sentence: " << endl;
	getline (cin, sent);
	splitSent (sent);
	checkSent ();
	
	findDups ();
	
	return 0;
}

.....
rest of code
.....


void findDups ()
{
	string newSent;
	for (unsigned int i = 0; i < words.size(); i++)
	 {
	 	newSent += words[i];
	 	newSent += " ";
	 }


I split the sentence into words and placed them in vector words. This is all I have so for this function before I got stuck. I was thinking about creating a new string and using pop_back to delete consecutive elements that are the same but I'm not too familiar with pop_back. Is that the right way to create this function or is there an easier way.
Are you allowed to use a set? Sets don't allow duplicates so this problem would be solved.
Have you thought of using maps? [http://www.cplusplus.com/reference/map/map/ ]. Maps are containers that can change their key value and mapped value. Same as vectors and sets, they also have iterators, but they are used a bit differently.

Here's a code example for finding duplicate words:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<map>
#include<string>
#include<vector>

using namespace std;

int main(){
vector <string> sentence;
string input;
while (cin>>input){
    sentence.push_back(input);
}
vector <string> dups;
map <string, unsigned int> counter_of_each_word;
for (int i=0; i<sentence.size(); i++){

    counter_of_each_word[sentence[i]]++;

    if (counter_of_each_word[sentence[i]]==2){
        dups.push_back(sentence[i]);
    }
}
for (int i=0; i<dups.size(); i++){
    cout<<dups[i]<<" ";
}
}
Are you looking for duplicate words or repeated words? In other words, if the sentence is "When did the dog eat the bone" then should you consider "the" to be a duplicate?

When you find duplicates, what should you do?
Topic archived. No new replies allowed.