Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. /* Dallin Smith, CS 142, Section 002, dallinsmith27@gmail.com
  8.  
  9. Test case #1
  10. INPUT:
  11. This is a test!
  12. y
  13.  
  14. EXPECTED OUTPUT:
  15. Welcome to the Simple Editor. Enter a string to be edited:
  16. This is a test!
  17.  
  18. Do you want to make this string a sentence (y/n)?
  19. This is a test!
  20.  
  21. Test case #2
  22. INPUT:
  23. this is a test
  24. n
  25. n
  26. find
  27. is
  28. delete
  29.  
  30. EXPECTED OUTPUT:
  31. Welcome to the Simple Editor. Enter a string to be edited:
  32. this is a test
  33.  
  34. Do you want to make this string a sentence (y/n)?
  35. Do you want to insert more text into your current text (y/n)?
  36. If you would like to find/replace or copy/paste, enter find or copy:
  37. Enter substring to find:
  38. Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?
  39. Final text is
  40. th is a test
  41.  
  42. Test case #3
  43. INPUT:
  44. this is a test
  45. n
  46. n
  47. find
  48. cheese
  49. delete
  50.  
  51. EXPECTED OUTPUT:
  52. Welcome to the Simple Editor. Enter a string to be edited:
  53. this is a test
  54.  
  55. Do you want to make this string a sentence (y/n)?
  56. Do you want to insert more text into your current text (y/n)?
  57. If you would like to find/replace or copy/paste, enter find or copy:
  58. Enter substring to find:
  59. Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?
  60. cheese was not found.
  61. Final text is
  62. this is a test
  63. */
  64.  
  65.  
  66. int main() {
  67.  
  68.  
  69. string userInput;
  70. string insertText;
  71. char userAnswer = 'n';
  72. int insertPos = 0;
  73. string userAnswer2;
  74. string userFind;
  75. string userAnswer3;
  76. string replaceInput;
  77. int copyPosition = 0;
  78. int copyLength = 0;
  79. int pastePosition = 0;
  80.  
  81. cout << "Welcome to the Simple Editor. Enter a string to be edited: " << endl;
  82. getline(cin,userInput);
  83. cout << userInput << endl << endl;
  84. cout << "Do you want to make this string a sentence (y/n)?" << endl;
  85. cin >> userAnswer;
  86.  
  87.  
  88. if (userAnswer == 'y'){
  89. userInput.at(0) = toupper(userInput.at(0));
  90. userInput.push_back('.');
  91. cout << userInput << endl << endl;
  92. }
  93. else{}
  94.  
  95. cout << "Do you want to insert more text into your current text (y/n)?" << endl;
  96. cin >> userAnswer;
  97. cin.ignore();
  98.  
  99. if (userAnswer == 'y'){
  100. cout << "Enter text to be inserted: " << endl;
  101. getline(cin, insertText);
  102. cout << "Enter position where text is to be inserted: " << endl;
  103. cin >> insertPos;
  104. cin.ignore();
  105. if (insertPos >=0 && insertPos <= userInput.size()){
  106. userInput = userInput.insert(insertPos, insertText);
  107. cout << userInput << endl << endl;
  108. }
  109.  
  110. else {
  111. cout << "No change made. Position must be non-negative and not exceed 14, the length of the current text." << endl;
  112. }
  113. }
  114.  
  115. else{}
  116.  
  117. cout << "If you would like to find/replace or copy/paste, enter find or copy: " << endl;
  118. getline(cin, userAnswer2);
  119.  
  120. if (userAnswer2 == "find"){
  121. cout << "Enter substring to find: " << endl;
  122. getline(cin, userFind);
  123. cout << "Do you want to find if/where the substring occurs, delete it, or replace it (find, delete, replace)?" << endl;
  124. getline(cin, userAnswer3);
  125.  
  126. if (userAnswer3 == "find"){
  127. if (userInput.find(userFind) == string::npos){
  128. cout << userFind << " was not found." << endl;
  129. }
  130. else {
  131. cout << userFind << " was found at position " << userInput.find(userFind) << "." << endl;
  132. }
  133. }
  134.  
  135. else if (userAnswer3 == "delete"){
  136. if (userInput.find(userFind) == string::npos){
  137. cout << userFind << " was not found. No change made." << endl;
  138. }
  139. else {userInput.replace(userInput.find(userFind),userFind.size(),"");
  140. }
  141. }
  142.  
  143. else if (userAnswer3 == "replace"){
  144. if (userInput.find(userFind) == string::npos){
  145. cout << userFind << " was not found. No change made." << endl;
  146. }
  147.  
  148. else {
  149. cout << "Enter replacement string: " << endl;
  150. getline(cin, replaceInput);
  151. userInput.replace(userInput.find(userFind),userFind.size(),replaceInput);
  152. }
  153. }
  154.  
  155. else {}
  156. }
  157.  
  158. else if (userAnswer2 == "copy"){
  159. cout << "Enter position and length of text to be copied, and position for paste: " << endl;
  160. cin >> copyPosition;
  161. cin >> copyLength ;
  162. cin >> pastePosition;
  163. if (copyPosition >= 0 && copyLength > 0 && pastePosition >= 0 && pastePosition <= userInput.length() && copyLength <= userInput.length()){
  164. userInput = userInput.insert(pastePosition, userInput.substr(copyPosition, copyLength));
  165. }
  166.  
  167. else {
  168. cout << "Values entered do not support copy/paste." << endl;
  169. }
  170. }
  171.  
  172. else{}
  173.  
  174. cout << "Final text is" << endl;
  175. cout << userInput << endl;
  176.  
  177.  
  178. return(0);
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement