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!