Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Bonus
- {
- private:
- string text;
- public:
- void SetValue(string value)
- {
- this->text = value;
- }
- string GetValue()
- {
- return this->text;
- }
- string reverse()
- {
- string reversedText;
- for (int i = text.length() - 1; i >= 0; i--)
- {
- reversedText += text[i];
- }
- text = reversedText;
- reversedText.clear();
- return text;
- }
- void add(char symbol)
- {
- text += symbol;
- }
- void addAt(char symbol, int index)
- {
- string textBeforeTheAddedSymbol;
- int indexesBeforeTheAddedSymbol = text.length() - 1 - index;
- for (size_t i = 0; i < indexesBeforeTheAddedSymbol; i++)
- {
- textBeforeTheAddedSymbol += text[i];
- }
- string textAfterTheAddedSymbol;
- for (size_t i = indexesBeforeTheAddedSymbol; i < text.length(); i++)
- {
- textAfterTheAddedSymbol += text[i];
- }
- text = textBeforeTheAddedSymbol + symbol + textAfterTheAddedSymbol;
- }
- void remove(char symbol)
- {
- for (size_t i = 0; i < text.length() - 1; i++)
- {
- if (text[i] == symbol)
- {
- text[i] = NULL;
- }
- }
- }
- void removeAt(char symbol, int index)
- {
- if (text[index + 1] == symbol)
- {
- text[index + 1] = NULL;
- }
- }
- bool contains(char symbol)
- {
- for (size_t i = 1; i < text.length() - 1; i++)
- {
- if (text[i] == symbol)
- {
- cout << "The word \"" << text << "\" contains the symbol \"" << symbol << "\"." << "\n";
- return true;
- }
- else
- {
- cout << "The word \"" << text << "\" doesn't contain the symbol \"" << symbol << "\"." << "\n";
- return false;
- }
- }
- }
- int containsCount(char symbol)
- {
- int counter = 0;
- for (size_t i = 0; i < text.length() - 1; i++)
- {
- if (text[i] == symbol)
- {
- counter++;
- }
- }
- return counter;
- }
- };
- int main()
- {
- Bonus text;
- string textToEnter;
- cout << "Enter a text: ";
- cin >> textToEnter;
- text.SetValue(textToEnter);
- cout << "\n";
- char symbolToAdd;
- cout << "Symbol to be added: ";
- cin >> symbolToAdd;
- text.add(symbolToAdd);
- cout << "\n";
- cout << "The word now looks like this: " << text.GetValue() << "\n";
- cout << "\n";
- cout << "Symbol to be added at index." << "\n";
- char symbolToAddAtIndex;
- cout << "Symbol: ";
- cin >> symbolToAddAtIndex;
- int indexToAdd;
- cout << "Index: ";
- cin >> indexToAdd;
- text.addAt(symbolToAddAtIndex, indexToAdd);
- cout << "\n";
- cout << "The word now looks like this: " << text.GetValue() << "\n";
- cout << "\n";
- char symbolToRemove;
- cout << "Symbol to be removed: ";
- cin >> symbolToRemove;
- text.remove(symbolToRemove);
- cout << "\n";
- cout << "The word now looks like this: " << text.GetValue() << "\n";
- cout << "\n";
- cout << "Symbol to be removed at index." << "\n";
- char symbolToRemoveAtIndex;
- cout << "Symbol: ";
- cin >> symbolToRemoveAtIndex;
- int indexToRemove;
- cout << "Index: ";
- cin >> indexToRemove;
- text.removeAt(symbolToRemoveAtIndex, indexToRemove);
- cout << "\n";
- cout << "The word now looks like this: " << text.GetValue() << "\n";
- cout << "\n";
- char symbolContains;
- cout << "Wanted symbol to be contained: ";
- cin >> symbolContains;
- cout << "\n";
- text.contains(symbolContains);
- cout << "\n";
- string timePeriod;
- text.containsCount(symbolContains) == 1 ? timePeriod = "time" : timePeriod = "times";
- cout << "The symbol \"" << symbolContains << "\" can be seen " << text.containsCount(symbolContains) << " " << timePeriod << " in the word \"" << text.GetValue() << "\"." << "\n" << "\n";
- cout << "When it's reversed, it looks like this: " << text.reverse() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement