1

I have to create program that takes text from file line-by-line, sorts it in lexicographic order and then if any word occured in previous line replace it with "-". So far I've got sorting figured out, but I feel like I've hit a wall with replacing words that occured before. My code so far looks like this:

#include <iostream>
#include <list>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;

int main()
{
    const string A = "abcdefghijklmnopqrstuwvxyz " ;
    list<string> lista;

    string line;

    ifstream myfile("./file.txt");

    while(!myfile.eof())
    {
        getline(myfile, line);
        lista.push_back(line);
    }

    vector<string> l;
    lista.sort();
    copy(begin(lista), end(lista), back_inserter(l));

    for(unsigned int i=0; i<l.size(); i++) // check if sorted properly
        cout << l[i] << endl;

    for(unsigned int i=0; i<l.size()-1; i++)
    {   
        unsigned int end=0;
        unsigned int j=1;

        while(l[i][end]==l[j][end])
        {
            end++;
            j++;
        }


        l[i].erase(0, end);
        l[i].insert(0, "- ");
    }

    for(unsigned int i=0; i<l.size(); i++)
        cout << l[i] << endl;

    return 0;
}

As you can see, I attempted doing this replacement thing, but to no success. Any help would be greatly appreciated!

  • 1
    Are you allowed to use standard algortihms and containers other than `std::list`? – Ted Lyngmo Jul 24 '22 at 09:19
  • 1
    Also read [Why is `iostream::eof()` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo Jul 24 '22 at 09:19
  • @TedLyngmo yes, I am. Will look into that thread as well. – operator_skrzyni_biegow Jul 24 '22 at 09:21
  • This might help, https://en.cppreference.com/w/cpp/algorithm/lexicographical_compare as does std::set (to keep list of unique words from last line) – Pepijn Kramer Jul 24 '22 at 09:21
  • I'm not sure I completely understand what you are trying to do. But compare your last loop with your problem statement '**if** any word occured in previous line replace it with "-"`. Where's the if statement in your last loop? There is none, you always replace, so that cannot be right. – john Jul 24 '22 at 09:22
  • 1
    And nice, finally a class that allows someone to use the full STL :) – Pepijn Kramer Jul 24 '22 at 09:22
  • @PepijnKramer will definitely look into that! – operator_skrzyni_biegow Jul 24 '22 at 09:22
  • Just extract the words from each line storing them in a `std::set` and checking the set from the previous line for matches... – fabian Jul 24 '22 at 09:23
  • @PepijnKramer Yes, hallelujah. – Paul Sanders Jul 24 '22 at 09:23
  • @operator_skrzyni_biegow One more style guide thing, [stop using `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) it might get you into trouble in large projects later. – Pepijn Kramer Jul 24 '22 at 09:28
  • @PepijnKramer I know that - I used it there only because I know it won't affect anything. Most of the time I code it like `using std::string, std::vector ` etc. Thanks for pointing out though! – operator_skrzyni_biegow Jul 24 '22 at 09:30
  • _I used it there only because I know it won't affect anything._ That's not a good reason. – Paul Sanders Jul 24 '22 at 17:34

0 Answers0