I am trying to use two strings to compare a list of words. For example, if someone inputs the words "Hello Man Tomato Yum Zebra"
I want to set my two vectors to be the first two words, then move the two vectors up. S1 would be hello
then S2 would be Man
. After comparing them and moving the vectors, S1 would now be Man
and S2 would be Tomato
. Keep doing this until I reach the last two words, and when I do, print out all the unique words we saw.
Only thing is I cannot store the entire list of words in a string and not using sets, vectors, or maps.
Let me know if you need more details or some of the code.
#include <iostream>
#include <string>
using namespace::std;
int main(){
string S;
string V;
string Unique;
string Dup;
cin >> S;
cin >> V;
int temp;
//For unique strings
if (S.length() != V.length()){
Unique += S;
Unique += ' ';
Unique += V;
}
else if (S[0] != V[0]){
Unique += S;
Unique += ' ';
Unique += V;
}
//for dup strings
else if (S[0] == V[0]){
for (int i=1; i < S.length(); ++i){
if (S[i] != V[i]) {
//if there is a letter that does match they are not duplicates so add them to unique
Unique += S;
Unique += ' ';
Unique += V;
break;
}
temp = i;
// this will break when it finds it's a dup or when i reaches the length of S and they are duplicates
}
if (temp == S.length()-1){
cout << "Duplicate string" << endl;
}
}
//cout the unique words
for (int i=0; i < Unique.length(); i++){
cout << Unique[i];
}
cout << endl;
return 0;
}
So essentially I am taking in two strings and comparing them to see if they are unique or duplicate. I want to be able to move vectors through input of more than two words, how would I have the vectors moving throughout the entire input without saving the entire input?
The expected output for an input of 3 words "Hello Hello Man"
would be just "Man"
since it is the only unique word.