Advertisement
PIBogdanov

C++ class using

Jun 5th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Bonus
  6. {
  7. private:
  8.  
  9.     string text;
  10.  
  11. public:
  12.  
  13.     void SetValue(string value)
  14.     {
  15.         this->text = value;
  16.     }
  17.  
  18.     string GetValue()
  19.     {
  20.         return this->text;
  21.     }
  22.  
  23.     string reverse()
  24.     {
  25.         string reversedText;
  26.  
  27.         for (int i = text.length() - 1; i >= 0; i--)
  28.         {
  29.             reversedText += text[i];
  30.         }
  31.  
  32.         text = reversedText;
  33.  
  34.         reversedText.clear();
  35.  
  36.         return text;
  37.     }
  38.  
  39.     void add(char symbol)
  40.     {
  41.         text += symbol;
  42.     }
  43.  
  44.     void addAt(char symbol, int index)
  45.     {
  46.         string textBeforeTheAddedSymbol;
  47.  
  48.         int indexesBeforeTheAddedSymbol = text.length() - 1 - index;
  49.  
  50.         for (size_t i = 0; i < indexesBeforeTheAddedSymbol; i++)
  51.         {
  52.             textBeforeTheAddedSymbol += text[i];
  53.         }
  54.  
  55.         string textAfterTheAddedSymbol;
  56.  
  57.         for (size_t i = indexesBeforeTheAddedSymbol; i < text.length(); i++)
  58.         {
  59.             textAfterTheAddedSymbol += text[i];
  60.         }
  61.  
  62.         text = textBeforeTheAddedSymbol + symbol + textAfterTheAddedSymbol;
  63.     }
  64.  
  65.     void remove(char symbol)
  66.     {
  67.         for (size_t i = 0; i < text.length() - 1; i++)
  68.         {
  69.             if (text[i] == symbol)
  70.             {
  71.                 text[i] = NULL;
  72.             }
  73.         }
  74.     }
  75.  
  76.     void removeAt(char symbol, int index)
  77.     {
  78.         if (text[index + 1] == symbol)
  79.         {
  80.             text[index + 1] = NULL;
  81.         }
  82.     }
  83.  
  84.     bool contains(char symbol)
  85.     {
  86.         for (size_t i = 1; i < text.length() - 1; i++)
  87.         {
  88.             if (text[i] == symbol)
  89.             {
  90.                 cout << "The word \"" << text << "\" contains the symbol \"" << symbol << "\"." << "\n";
  91.                 return true;
  92.             }
  93.  
  94.             else
  95.             {
  96.                 cout << "The word \"" << text << "\" doesn't contain the symbol \"" << symbol << "\"." << "\n";
  97.                 return false;
  98.             }
  99.         }
  100.     }
  101.  
  102.     int containsCount(char symbol)
  103.     {
  104.         int counter = 0;
  105.  
  106.         for (size_t i = 0; i < text.length() - 1; i++)
  107.         {
  108.             if (text[i] == symbol)
  109.             {
  110.                 counter++;
  111.             }
  112.         }
  113.  
  114.         return counter;
  115.     }
  116. };
  117.  
  118. int main()
  119. {
  120.     Bonus text;
  121.  
  122.     string textToEnter;
  123.  
  124.     cout << "Enter a text: ";
  125.     cin >> textToEnter;
  126.  
  127.     text.SetValue(textToEnter);
  128.  
  129.     cout << "\n";
  130.  
  131.     char symbolToAdd;
  132.  
  133.     cout << "Symbol to be added: ";
  134.     cin >> symbolToAdd;
  135.  
  136.     text.add(symbolToAdd);
  137.  
  138.     cout << "\n";
  139.  
  140.     cout << "The word now looks like this: " << text.GetValue() << "\n";
  141.  
  142.     cout << "\n";
  143.  
  144.     cout << "Symbol to be added at index." << "\n";
  145.  
  146.     char symbolToAddAtIndex;
  147.  
  148.     cout << "Symbol: ";
  149.     cin >> symbolToAddAtIndex;
  150.  
  151.     int indexToAdd;
  152.  
  153.     cout << "Index: ";
  154.     cin >> indexToAdd;
  155.  
  156.     text.addAt(symbolToAddAtIndex, indexToAdd);
  157.  
  158.     cout << "\n";
  159.  
  160.     cout << "The word now looks like this: " << text.GetValue() << "\n";
  161.  
  162.     cout << "\n";
  163.  
  164.     char symbolToRemove;
  165.  
  166.     cout << "Symbol to be removed: ";
  167.     cin >> symbolToRemove;
  168.  
  169.     text.remove(symbolToRemove);
  170.  
  171.     cout << "\n";
  172.  
  173.     cout << "The word now looks like this: " << text.GetValue() << "\n";
  174.  
  175.     cout << "\n";
  176.  
  177.     cout << "Symbol to be removed at index." << "\n";
  178.  
  179.     char symbolToRemoveAtIndex;
  180.  
  181.     cout << "Symbol: ";
  182.     cin >> symbolToRemoveAtIndex;
  183.  
  184.     int indexToRemove;
  185.  
  186.     cout << "Index: ";
  187.     cin >> indexToRemove;
  188.  
  189.     text.removeAt(symbolToRemoveAtIndex, indexToRemove);
  190.  
  191.     cout << "\n";
  192.  
  193.     cout << "The word now looks like this: " << text.GetValue() << "\n";
  194.  
  195.     cout << "\n";
  196.  
  197.     char symbolContains;
  198.  
  199.     cout << "Wanted symbol to be contained: ";
  200.     cin >> symbolContains;
  201.  
  202.     cout << "\n";
  203.  
  204.     text.contains(symbolContains);
  205.  
  206.     cout << "\n";
  207.  
  208.     string timePeriod;
  209.  
  210.     text.containsCount(symbolContains) == 1 ? timePeriod = "time" : timePeriod = "times";
  211.  
  212.     cout << "The symbol \"" << symbolContains << "\" can be seen " << text.containsCount(symbolContains) << " " << timePeriod << " in the word \"" << text.GetValue() << "\"." << "\n" << "\n";
  213.  
  214.     cout << "When it's reversed, it looks like this: " << text.reverse() << "\n";
  215.  
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement