Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- /* Dallin Smith, CS 142, Section 002, dallinsmith27@gmail.com
- Test case #1
- INPUT:
- This is a test!
- y
- EXPECTED OUTPUT:
- Welcome to the Simple Editor. Enter a string to be edited:
- This is a test!
- Do you want to make this string a sentence (y/n)?
- This is a test!
- Test case #2
- INPUT:
- this is a test
- n
- n
- find
- is
- delete
- EXPECTED OUTPUT:
- Welcome to the Simple Editor. Enter a string to be edited:
- this is a test
- Do you want to make this string a sentence (y/n)?
- Do you want to insert more text into your current text (y/n)?
- If you would like to find/replace or copy/paste, enter find or copy:
- Enter substring to find:
- Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?
- Final text is
- th is a test
- Test case #3
- INPUT:
- this is a test
- n
- n
- find
- cheese
- delete
- EXPECTED OUTPUT:
- Welcome to the Simple Editor. Enter a string to be edited:
- this is a test
- Do you want to make this string a sentence (y/n)?
- Do you want to insert more text into your current text (y/n)?
- If you would like to find/replace or copy/paste, enter find or copy:
- Enter substring to find:
- Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?
- cheese was not found.
- Final text is
- this is a test
- */
- int main() {
- string userInput;
- string insertText;
- char userAnswer = 'n';
- int insertPos = 0;
- string userAnswer2;
- string userFind;
- string userAnswer3;
- string replaceInput;
- int copyPosition = 0;
- int copyLength = 0;
- int pastePosition = 0;
- cout << "Welcome to the Simple Editor. Enter a string to be edited: " << endl;
- getline(cin,userInput);
- cout << userInput << endl << endl;
- cout << "Do you want to make this string a sentence (y/n)?" << endl;
- cin >> userAnswer;
- if (userAnswer == 'y'){
- userInput.at(0) = toupper(userInput.at(0));
- userInput.push_back('.');
- cout << userInput << endl << endl;
- }
- else{}
- cout << "Do you want to insert more text into your current text (y/n)?" << endl;
- cin >> userAnswer;
- cin.ignore();
- if (userAnswer == 'y'){
- cout << "Enter text to be inserted: " << endl;
- getline(cin, insertText);
- cout << "Enter position where text is to be inserted: " << endl;
- cin >> insertPos;
- cin.ignore();
- if (insertPos >=0 && insertPos <= userInput.size()){
- userInput = userInput.insert(insertPos, insertText);
- cout << userInput << endl << endl;
- }
- else {
- cout << "No change made. Position must be non-negative and not exceed 14, the length of the current text." << endl;
- }
- }
- else{}
- cout << "If you would like to find/replace or copy/paste, enter find or copy: " << endl;
- getline(cin, userAnswer2);
- if (userAnswer2 == "find"){
- cout << "Enter substring to find: " << endl;
- getline(cin, userFind);
- cout << "Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?" << endl;
- getline(cin, userAnswer3);
- if (userAnswer3 == "find"){
- if (userInput.find(userFind) == string::npos){
- cout << userFind << " was not found." << endl;
- }
- else {
- cout << userFind << " was found at position " << userInput.find(userFind) << "." << endl;
- }
- }
- else if (userAnswer3 == "delete"){
- if (userInput.find(userFind) == string::npos){
- cout << userFind << " was not found. No change made." << endl;
- }
- else {userInput.replace(userInput.find(userFind),userFind.size(),"");
- }
- }
- else if (userAnswer3 == "replace"){
- if (userInput.find(userFind) == string::npos){
- cout << userFind << " was not found. No change made." << endl;
- }
- else {
- cout << "Enter replacement string: " << endl;
- getline(cin, replaceInput);
- userInput.replace(userInput.find(userFind),userFind.size(),replaceInput);
- }
- }
- else {}
- }
- else if (userAnswer2 == "copy"){
- cout << "Enter position and length of text to be copied, and position for paste: " << endl;
- cin >> copyPosition;
- cin >> copyLength ;
- cin >> pastePosition;
- if (copyPosition >= 0 && copyLength > 0 && pastePosition >= 0 && pastePosition <= userInput.length() && copyLength <= userInput.length()){
- userInput = userInput.insert(pastePosition, userInput.substr(copyPosition, copyLength));
- }
- else {
- cout << "Values entered do not support copy/paste." << endl;
- }
- }
- else{}
- cout << "Final text is" << endl;
- cout << userInput << endl;
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement