Y2ok
Y2ok

Reputation: 171

Script that replace each repeated letter in a string with one letter

I have a script which takes a word from input file and outputs customizated word in output file.

For example, in input file I have word ccccccaaaaaannnnndlllllleeeeeand I need to edit the word it to look like candle and output in other file. So could you please give me a example of it?

So far I have:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
    string vards;
    std::ifstream input_file("virkne3.in");
    input_file >> vards;
    std::ofstream output_file("virkne3.out");
    output_file << vards.erase(std::unique(vards.begin(), vards.end()), vards.end());
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}

Upvotes: 0

Views: 108

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103733

Assuming you mean consecutive repeated letters, std::unique does exactly that.

vards.erase(std::unique(vards.begin(), vards.end()), vards.end());

Upvotes: 3

Related Questions