#include <iostream>
#include <string>

using namespace std;

struct Codedtext
{
    string text;
    Codedtext* next = nullptr;
};

using CodedtextPtr = Codedtext*;

CodedtextPtr createCodeList()
{
    CodedtextPtr codeList = nullptr;
    CodedtextPtr *node = &codeList;
    string input;

    cout << "Enter your coded text: " << endl;
    getline(cin, input);
   
    string::size_type start = 0, end;
    while ((end = input.find("pe", start)) != string::npos)
    {
        *node = new Codedtext{ input.substr(start, end-start) };
        node = &((*node)->next);

        *node = new Codedtext{ "pe" };
        node = &((*node)->next);

        start = end + 2;
    }
    if (start < input.size())
        *node = new Codedtext{ input.substr(start) };

    return codeList;
}

void removeCodeWords(CodedtextPtr& codeList)
{
    CodedtextPtr* prev = &codeList;
    
    for(auto it = codeList; it != nullptr; )
    {
        CodedtextPtr next = it->next;

        if (it->text == "pe")
        {
        	*prev = next;
            delete it;
        }
        else
            prev = &(it->next);

        it = next;
    }
}

void printCodeList(CodedtextPtr codeList)
{
    while (codeList)
    {
        cout << codeList->text;
        codeList = codeList->next;
    }
    cout << endl;
}

void freeCodeList(CodedtextPtr codeList)
{
    while (codeList)
    {
        CodedtextPtr next = codeList->next;
        delete codeList;
        codeList = next;
    }
}

int main()
{
    CodedtextPtr codeList = createCodeList();
    removeCodeWords(codeList);
    printCodeList(codeList);
    freeCodeList(codeList);
    return 0;
}