Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <string>
- #include <vector>
- using namespace std;
- /*
- This program creates a silly substitution story by
- replacing words in normal text with words
- provided by user.
- In a vector, the element at each index below
- should be the type of word needed at different
- places in the story. The user is
- prompted to enter the right type of word.
- 0 - part of the body
- 1 - adjective
- 2 - animal
- 3 - different part of the body
- 4 - silly word
- 5 - cartoon character
- 6 - silly word, start with a capital letter
- 7 - verb
- The story is one string. Each substitution point in
- the string is marked with a number that corresponds
- to the array index of the word for the substitution.
- */
- int main()
- {
- // This is the story which will be have words inserted to
- // make it sound silly.
- string story = "New, [0]-less dinosaur discovered.\n"
- "\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 "
- "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 "
- "one [0]. Now all that has changed.\n"
- "\n\"With the discovery of this [0]-less dinosaur, we now have to re-examine [0] loss for all members of [4],\" "
- "says [5], a dinosaur enthusiast and founder of the [6] Dinosaur Institute. "
- "\"This discovery is going to revolutionize the way we [7] and how we see these amazing pre-historic creatures.\"\n";
- vector<string> substitutions; // List of words provided by the user to substitute into the story.
- string input; // User input.
- cout << "Let's create a silly substitution story!\n"
- "Enter 8 words to substute into a normal story, to make the story silly:\n";
- cout << "1. a part of the body: "; // Request the type of word needed for the story.
- getline(cin, input); // Get user input.
- substitutions.push_back(input); // Add user input to list of words to substitute.
- cout << "2. an adjective: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "3. an amimal: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "4. a different part of the body: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "5. a silly word: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "6. a cartoon character: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "7. a different silly word, starting with a capital letter: ";
- getline(cin, input);
- substitutions.push_back(input);
- cout << "8. a verb: ";
- getline(cin, input);
- substitutions.push_back(input);
- string marker; // Some text in the story that marks where to insert silly words.
- string opening_bracket = "["; // Used to make the marker.
- string closing_bracket = "]"; // Used to make the marker.
- string index_as_string; // The vector index is converted to a string for the marker.
- // Take each item in the substitutions vector, in order,
- for (size_t i = 0; i < substitutions.size(); ++i)
- {
- // Create the substitution marker to search for.
- // It is the vector index in square brackets, like [3]
- index_as_string = to_string(i); // Make a string version of the current index.
- // Assemble the marker.
- marker = opening_bracket + index_as_string + closing_bracket;
- size_t search_start = 0; // Start searching at the beginning of the story.
- size_t marker_location = 0; // This stores the location when the marker if found
- bool whole_string_searched = false; // Assume the string is not searched yet.
- // Go through the story, character by character,
- while (whole_string_searched == false)
- {
- // Locate the marker
- marker_location = story.find(marker, search_start);
- if (marker_location != std::string::npos) // If we haven't reached the end of the story,
- {
- // Replace the marker with the text from the vector, at the current index.
- story.replace(marker_location, marker.size(), substitutions[i]);
- search_start = marker_location; // Continue searching from here.
- }
- // Otherwise, we've reached the end of the string, so indicate this.
- else whole_string_searched = true;
- }
- }
- cout << "\nHere's the silly story:\n\n"; // Announce the silly story is created.
- cout << story << '\n'; // Display the silly story.
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement