fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Codedtext
  7. {
  8. string text;
  9. Codedtext* next = nullptr;
  10. };
  11.  
  12. using CodedtextPtr = Codedtext*;
  13.  
  14. CodedtextPtr createCodeList()
  15. {
  16. CodedtextPtr codeList = nullptr;
  17. CodedtextPtr *node = &codeList;
  18. string input;
  19.  
  20. cout << "Enter your coded text: " << endl;
  21. getline(cin, input);
  22.  
  23. string::size_type start = 0, end;
  24. while ((end = input.find("pe", start)) != string::npos)
  25. {
  26. *node = new Codedtext{ input.substr(start, end-start) };
  27. node = &((*node)->next);
  28.  
  29. *node = new Codedtext{ "pe" };
  30. node = &((*node)->next);
  31.  
  32. start = end + 2;
  33. }
  34. if (start < input.size())
  35. *node = new Codedtext{ input.substr(start) };
  36.  
  37. return codeList;
  38. }
  39.  
  40. void removeCodeWords(CodedtextPtr& codeList)
  41. {
  42. CodedtextPtr* prev = &codeList;
  43.  
  44. for(auto it = codeList; it != nullptr; )
  45. {
  46. CodedtextPtr next = it->next;
  47.  
  48. if (it->text == "pe")
  49. {
  50. *prev = next;
  51. delete it;
  52. }
  53. else
  54. prev = &(it->next);
  55.  
  56. it = next;
  57. }
  58. }
  59.  
  60. void printCodeList(CodedtextPtr codeList)
  61. {
  62. while (codeList)
  63. {
  64. cout << codeList->text;
  65. codeList = codeList->next;
  66. }
  67. cout << endl;
  68. }
  69.  
  70. void freeCodeList(CodedtextPtr codeList)
  71. {
  72. while (codeList)
  73. {
  74. CodedtextPtr next = codeList->next;
  75. delete codeList;
  76. codeList = next;
  77. }
  78. }
  79.  
  80. int main()
  81. {
  82. CodedtextPtr codeList = createCodeList();
  83. removeCodeWords(codeList);
  84. printCodeList(codeList);
  85. freeCodeList(codeList);
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 5460KB
stdin
Hepello Ipe hapeve ape prpeobpelepem
stdout
Enter your coded text: 
Hello I have a problem