-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_editor_lab3_CS_142.cpp
178 lines (127 loc) · 5.19 KB
/
string_editor_lab3_CS_142.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Jordan Holbrook, Section 6,
CS 142 Lab 3, [email protected]
Test case 1:
Test Case 2:
Test Case 3:
*/
#include <iostream>
using namespace std;
#include <cctype>
int main() {
/* Part 1 Prompt the user to input the initial text to be edited. See the examples below for exact I/O format.
You will enter just one line of text. Use getline to put the text into a string variable.
Strings can be any characters, numbers, punctuation, etc. Then print out the string.*/
string userInput;
cout << "Welcome to the Simple Editor. Enter a string to be edited: " << endl;
getline(cin, userInput);
cout << userInput << endl;
/* Part 2 Output "Do you want to make this string a sentence (y/n)? ". If yes:*/
string userInput2;
int lastIndex;
char lastChar;
cout << "Do you want to make this string a sentence (y/n)? " << endl;
cin >> userInput2;
cin.ignore();
if (userInput2 == "y") {
userInput.at(0) = toupper(userInput.at(0));
lastIndex = userInput.size() - 1;
lastChar = userInput.at(lastIndex);
if ( (lastChar != '.') && (lastChar != '!') && (lastChar != '?') ) {
userInput.append(".");
}
cout << userInput << endl;
}
/* Part 3 Output "Do you want to insert more text into your current text (y/n)? " If yes,
Output "Enter text to be inserted: ". Use getline to get text.
Output "Enter position where text is to be inserted: ". Get position using cin.
Insert the text and print out the updated string. */
string userInput3;
string userInput4;
int positionPreference;
cout << "Do you want to insert more text into your current text (y/n)? " << endl;
cin >> userInput3;
cin.ignore();
if (userInput3 == "y") {
cout << "Enter text to be inserted: " << endl;
getline(cin, userInput4);
cout << "Enter position where text is to be inserted: " << endl;
cin >> positionPreference;
cin.ignore();
if ((positionPreference < 0)||(positionPreference > userInput.size())){
cout << "No change made. Position must be non-negative and not exceed";
cout << userInput.size() << " , the length of the current text." << endl;
}
else {
cout << userInput.insert(positionPreference, " " + userInput4);
}
}
string userInput7;
string userInput8;
/* Part 4 Output If they enter anything other than "find" or "copy",
just continue to the end of the program.*/
cout << "If you would like to find/replace or copy/paste, enter find or copy: " << endl;
string userInput5;
getline(cin, userInput5);
if (userInput5 == "find") {
cout << "Enter substring to find: " << endl;
string userInput6;
getline(cin, userInput6);
int lengthInput6 = userInput6.size();
int positionInput6 = userInput.find(userInput6);
cout << "Do you want to find if/where the substring occurs, delete it,";
cout << "or replace it (find, delete, replace)? " << endl;
string userInput7;
getline(cin, userInput7);
if (userInput7 == "find") {
if (userInput.find(userInput6) != string::npos) {
cout << userInput6 << " was found at position " << positionInput6 << "." << endl;
}
else {
cout << userInput6 << " was not found." << endl;
}
}
if (userInput7 == "delete") {
if (userInput.find(userInput6) != string::npos) {
userInput.erase(positionInput6, lengthInput6);
}
else {
cout << userInput6 << " was not found. No change made. " << endl;
}
}
if (userInput7 == "replace") {
if (userInput.find(userInput6) != string::npos) {
cout << "Enter replacement string:" << endl;
getline(cin, userInput8);
userInput.replace(userInput.find(userInput6), lengthInput6, userInput8);
}
else {
cout << userInput6 << " was not found. No change made. " << endl;
}
}
}
else if (userInput5 == "copy") {
cout << "Enter position and length of text to be copied, and position for paste: " << endl;
int positionCopy;
int lengthStringCopy;
int positionStringPaste;
cin >> positionCopy;
cin.ignore();
cin >> lengthStringCopy;
cin.ignore();
cin >> positionStringPaste;
cin.ignore();
if ((positionCopy < 0) || (positionCopy >= userInput.size()) || (positionStringPaste > userInput.size())
|| (lengthStringCopy > userInput.size()) || (positionStringPaste < 0) || (lengthStringCopy < 0) ||
(positionCopy + lengthStringCopy > userInput.size())) {
cout << "Values entered do not support copy/paste." << endl;
}
else {
string copiedText;
copiedText = userInput.substr(positionCopy, lengthStringCopy);
userInput.insert(positionStringPaste, copiedText);
}
}
cout << "Final text is" << endl;
cout << userInput << endl;
return 0;
}