-2

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.

Almat
  • 3
  • 3
  • You can store a list of the unique words though, right? If that is the case what you want is a `std::set`. – NathanOliver Apr 02 '18 at 18:54
  • 2
    This is a help with coding site. show us the code you need help with please. – Jacobr365 Apr 02 '18 at 18:54
  • Once you have found [The most elegant way to iterate the words of a string](https://stackoverflow.com/questions/236129/the-most-elegant-way-to-iterate-the-words-of-a-string) then just loop to look at the words at index (i) and (i+1) and put them in a std::set. – Wyck Apr 02 '18 at 18:57
  • Try to do it a C++ way, using `std::string`, `std::vector`, and others... When that's working, dumb it down to suit your teacher. In that way, you will be less likely to acquire bad habits. Why did I never think of that suggestion before? – Jive Dadson Apr 02 '18 at 19:12
  • 3
    What's the reason for comparing word neighbors ? What do you use the outcome of the comparison for ? What is the expected output for your example word list ? What other cases do you have ? Also, show your code. – Sid S Apr 02 '18 at 19:14
  • Not just "some of code".- Post a [MCVE]. Read [ask]. You also need more comprehensive examples of input and expected output. And what does "comparing them" mean? Checking for repeats like "Man Man"? – Jive Dadson Apr 02 '18 at 19:16
  • Do you have a question? – Galik Apr 02 '18 at 19:31
  • @SidS I updated with expected output, the outcome of the comparison is just a list of the unique words nothing special. The input is in sorted order so I only have to compare the words next to each other. – Almat Apr 02 '18 at 19:38
  • That's progress. Now, what's your question? Re-read [ask]. – Jive Dadson Apr 02 '18 at 20:07
  • Will your teacher let you define functions? – Jive Dadson Apr 02 '18 at 20:08
  • @JiveDadson I just updated it. And yes, I don't see why not. Only rules were no vectors, sets, or maps. I also cannot store the entire list of words. – Almat Apr 02 '18 at 20:10
  • That code is only a fragment. It will not compile. Re-read how to post a [MCVE]. – Jive Dadson Apr 02 '18 at 20:13
  • @JiveDadson I added the #include's and the main. Does that compile for you now? – Almat Apr 02 '18 at 20:16

1 Answers1

0

You could have a function that with each successive call generates the next word from the input. In a loop, have two calls that compare the last word with the next word until the end of the input is reached.

#include <iostream>
#include <string>

using namespace std;

//global variables; when updated
//return the next word from nextWord function
int start, space;

string nextWord(string wordList)
{
    //find the next space
    space = wordList.find(" ", space+1);
    //find the next word
    string word = wordList.substr(start, space-start);
    //restrict the search range
    start = space + 1;
    return word;
}
int main()
{
    //set initial values
    string words;
    getline(cin, words);
    string last = nextWord(words);
    string next;

    while (true)
    {
        if (space == -1)
            break;
        next = nextWord(words);
        if (last == next){
            cout << next << endl;
        }
        last = next;
    }
    return 0;
}
jackw11111
  • 1,457
  • 1
  • 17
  • 34