Advertisement
janac

Silly Substitution Story

Dec 16th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8. This program creates a silly substitution story by
  9. replacing words in normal text with words
  10. provided by user.
  11.  
  12. In a vector, the element at each index below
  13. should be the type of word needed at different
  14. places in the story. The user is
  15. prompted to enter the right type of word.
  16. 0 - part of the body
  17. 1 - adjective
  18. 2 - animal
  19. 3 - different part of the body
  20. 4 - silly word
  21. 5 - cartoon character
  22. 6 - silly word, start with a capital letter
  23. 7 - verb
  24. The story is one string. Each substitution point in
  25. the string is marked with a number that corresponds
  26. to the array index of the word for the substitution.
  27. */
  28.  
  29. int main()
  30. {
  31.     // This is the story which will be have words inserted to
  32.     // make it sound silly.
  33.     string story = "New, [0]-less dinosaur discovered.\n"
  34.         "\nLast week, a team of scientists discovered a 5-miilion-year-old fossil with a very [1], [2]-like [3]. They are excited to announce that "
  35.         "they\'ve found a new species of [0]-less dinosaur. It seems to belong to the sub-group [4], in which all members had at least "
  36.         "one [0]. Now all that has changed.\n"
  37.         "\n\"With the discovery of this [0]-less dinosaur, we now have to re-examine [0] loss for all members of [4],\" "
  38.         "says [5], a dinosaur enthusiast and founder of the [6] Dinosaur Institute. "
  39.         "\"This discovery is going to revolutionize the way we [7] and how we see these amazing pre-historic creatures.\"\n";
  40.     vector<string> substitutions; // List of words provided by the user to substitute into the story.
  41.     string input; // User input.
  42.  
  43.     cout << "Let's create a silly substitution story!\n"
  44.     "Enter 8 words to substute into a normal story, to make the story silly:\n";
  45.     cout << "1. a part of the body: "; // Request the type of word needed for the story.
  46.     getline(cin, input); // Get user input.
  47.     substitutions.push_back(input); // Add user input to list of words to substitute.
  48.     cout << "2. an adjective: ";
  49.     getline(cin, input);
  50.     substitutions.push_back(input);
  51.     cout << "3. an amimal: ";
  52.     getline(cin, input);
  53.     substitutions.push_back(input);
  54.     cout << "4. a different part of the body: ";
  55.     getline(cin, input);
  56.     substitutions.push_back(input);
  57.     cout << "5. a silly word: ";
  58.     getline(cin, input);
  59.     substitutions.push_back(input);
  60.     cout << "6. a cartoon character: ";
  61.     getline(cin, input);
  62.     substitutions.push_back(input);
  63.     cout << "7. a different silly word, starting with a capital letter: ";
  64.     getline(cin, input);
  65.     substitutions.push_back(input);
  66.     cout << "8. a verb: ";
  67.     getline(cin, input);
  68.     substitutions.push_back(input);
  69.  
  70.     string marker; // Some text in the story that marks where to insert silly words.
  71.     string opening_bracket = "["; // Used to make the marker.
  72.     string closing_bracket = "]"; // Used to make the marker.
  73.     string index_as_string; // The vector index is converted to a string for the marker.
  74.    
  75.     // Take each item in the substitutions vector, in order,
  76.     for (size_t i = 0; i < substitutions.size(); ++i)
  77.     {
  78.         // Create the substitution marker to search for.
  79.         // It is the vector index in square brackets, like [3]
  80.         index_as_string = to_string(i); // Make a string version of the current index.
  81.         // Assemble the marker.
  82.         marker = opening_bracket + index_as_string + closing_bracket;
  83.  
  84.         size_t search_start = 0; // Start searching at the beginning of the story.
  85.         size_t marker_location = 0; // This stores the location when the marker if found
  86.         bool  whole_string_searched = false; // Assume the string is not searched yet.
  87.         // Go through the story, character by character,
  88.         while (whole_string_searched == false)
  89.         {
  90.             // Locate the marker
  91.             marker_location = story.find(marker, search_start);
  92.             if (marker_location != std::string::npos) // If we haven't reached the end of the story,
  93.             {
  94.                 // Replace the marker with the text from the vector, at the current index.
  95.                 story.replace(marker_location, marker.size(), substitutions[i]);
  96.                 search_start = marker_location; // Continue searching from here.
  97.             }
  98.             // Otherwise, we've reached the end of the string, so indicate this.
  99.             else whole_string_searched = true;
  100.         }    
  101.     }
  102.  
  103.     cout << "\nHere's the silly story:\n\n"; // Announce the silly story is created.
  104.     cout << story << '\n'; // Display the silly story.
  105.    
  106.  
  107.     return 0; // End the program.
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement