Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- #include<string>
- #include <set>
- #include <algorithm>
- using namespace std;
- bool Choic() {
- bool IsCorrect = true;
- string Choice;
- bool Console = true;
- while(IsCorrect) {
- cout << "If you want to enter data from the console, enter Y or y, and if from the file, enter N or n\n";
- cin >> Choice;
- if ((Choice == "Y") || (Choice == "y") || (Choice == "N") || (Choice == "n")) {
- IsCorrect = false;
- if ((Choice == "Y") || (Choice == "y")) {
- Console = true;
- }
- else {
- Console = false;
- }
- }
- else {
- IsCorrect = true;
- cout << "This is mistake\n";
- }
- }
- return Console;
- }
- string EnterText() {
- cout << "Enter the text. All special characters and numbers such as 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, /, -, _, =, +, @, #, $, %, ^, &, *, (, ) will be deleted.\n";
- string Text;
- while (getline(cin, Text))
- return Text;
- }
- string EnterFileText() {
- ifstream fin("D:\\input.txt");
- string Text;
- while (getline(fin, Text))
- return Text;
- }
- string EditText(bool Choise) {
- set<char> InvalidValues {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '"', ']', '}', '}', '[', '/', '-', '_', '=', '+', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
- set<char> Values {'!', ';', ':', ',', '.', '?'};
- string Text;
- if (Choise) {
- Text = EnterText();
- }
- else {
- Text = EnterFileText();
- }
- int Lengt;
- int I = 0;
- Lengt = size(Text);
- while (I <= Lengt) {
- if (InvalidValues.count(Text[I])) {
- Text.erase(I,1);
- I--;
- Lengt--;
- }
- if ((Text[I] == ' ') && (Text[I] + 1 == ' ')){
- Text.erase(I,1);
- I--;
- Lengt--;
- }
- if ((Values.count(Text[I])) && (Values.count(Text[I + 1]))) {
- Text.erase(I,1);
- I--;
- Lengt--;
- }
- I++;
- }
- return Text;
- }
- void Encryption(bool Choice) {
- ofstream fout("D:\\output.txt");
- set<char> Values {'!', ';', ':', ',', '.', '?'};
- string Text = EditText(Choice);
- int Lengt = size(Text);
- int Count = 0;
- int I = 0;
- string Temp;
- int Parity = 0;
- while (I < Lengt) {
- Count++;
- if (Text[I] == ' ') {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substr(I - (Count - 1), Count - 1);
- for (int J = 0; J < Count; J++) {
- Temp[J] = toupper(Temp[J]);
- }
- Text.erase(I - (Count - 1), Count - 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Text.insert(I - (Count - 1), 1, '(');
- Text.insert(I + 1, 1, ')');
- Lengt = Lengt + 2;
- I = I + 2;
- Count = 0;
- }
- if (Values.count(Text[I + 1])) {
- I++;
- }
- }
- if ((Values.count(Text[I])) && (I < Lengt) && (Text[I - 1] != ' ')) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substr(I - (Count - 1), Count - 1);
- for (int J = 0; J < Count; J++) {
- Temp[J] = toupper(Temp[J]);
- }
- Text.erase(I - (Count - 1), Count - 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Text.insert(I - (Count - 1), 1, '(');
- Text.insert(I + 1, 1, ')');
- Lengt = Lengt + 2;
- I = I + 2;
- Count = 0;
- }
- if (Values.count(Text[I + 1])) {
- I++;
- }
- }
- if ((I == Lengt) && (Values.count(Text[I]))) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substr(I - (Count - 1), Count - 1);
- for (int J = 0; J < Count; J++) {
- Temp[J] = toupper(Temp[J]);
- }
- Text.erase(I - (Count - 1), Count - 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Lengt = Lengt + 2;
- Text.insert(I - (Count - 1), 1, '(');
- Text.insert(I + 1, 1, ')');
- I = I + 2;
- Count = 0;
- }
- }
- else {
- if (I == Lengt - 1) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substr(I - Count, Count + 1);
- for (int J = 0; J < Count; J++) {
- Temp[J] = toupper(Temp[J]);
- }
- Text.erase(I - Count, Count + 1);
- Text.insert(I - Count, Temp);
- Count = 0;
- }
- else {
- Lengt = Lengt + 2;
- Text.insert(I - (Count - 1), 1, '(');
- Text.insert(I + 2, 1, ')');
- I = I + 2;
- Count = 0;
- }
- }
- }
- I++;
- }
- if (Choice) {
- cout << Text;
- } else {
- cout << Text;
- fout << Text;
- }
- }
- void body() {
- bool Choice = Choic();
- Encryption(Choice);
- }
- int main() {
- cout << "This program in the text in every even word replaces all lowercase alphabetic characters with uppercase, and each odd word is enclosed in parentheses";
- body();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment