Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need to replace words in sentences.

+1 vote
asked Jun 30, 2021 by Miguel Lara (130 points)
Nothing has worked and I messed up somewhere along the line please help me figure out where I went wrong. I'm mainly supposed to use do while and while loops.Other fruits are to be replaced by pineapple.

#include <iostream>

#include <vector>

#include <string>

using namespace std;

int main()

{

vector<string> fruits; //what fruits are in the program

fruits.push_back("apple");

fruits.push_back("banana");

fruits.push_back("cherry");

fruits.push_back("coconut");

fruits.push_back("grape");

fruits.push_back("lime");

fruits.push_back("orange");

fruits.push_back("pineapple");

string input, Yes;

string input, no;

string sentence;

cout << "\n============================";

cout << "\n\nWelcome to our Fruit Finder";

cout << "\n\n============================";

cout << "\nYour fruit list contains: ";

cout << "apple, ";

cout << "banana, ";

cout << "cherry, ";

cout << "coconut, ";

cout << "grape, ";

cout << "lime, ";

cout << "orange.\n";

cout << "Please enter a sentence containing a fruit from the list above: ";

cin >> sentence;

cout << "Would you like to replace a fruit from your sentence?\n";

cout << "\nYes or no?\n" << endl;

cin >> input;

while (input == "Yes")

cout << "Which fruit will you replace?";

if ("apple") {

cout << "apple will be replaced by pineapple!";

}

else if ("banana") {

cout << "banana will be replaced by pineapple!";

}

else if ("cherry") {

cout << "cherry will be replaced by pineapple!";

}

else if ("coconut") {

cout << "coconut will be replaced by pineapple!";

}

else if ("grape") {

cout << "grape will be replaced by pineapple!";

}

else if ("lime") {

cout << "lime will be replaced by pineapple!";

}

else if ("orange") {

cout << "orange will be replaced by pineapple!";

}

if (input == "no")

cout << "You are not replacing fruit!";

return 0;

}

1 Answer

0 votes
answered Sep 29, 2021 by Peter Minarik (63,180 points)

Here's a possible solution for the problem:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> fruits = // what fruits are in the program (pineapple is not part of this vector)
    {
        "apple",
        "banana",
        "cherry",
        "coconut",
        "grape",
        "lime",
        "orange"
    };

    std::cout << "===========================" << std::endl << std::endl;
    std::cout << "Welcome to our Fruit Finder" << std::endl << std::endl;
    std::cout << "===========================" << std::endl << std::endl;
    std::cout << "Your fruit list contains: ";
    
    bool first = true;
    for (std::string fruit : fruits)
    {
        if (first)
            first = false;
        else
            std::cout << ", ";
        std::cout << fruit;
    }

    std::cout << std::endl << std::endl << "Please enter a sentence containing a fruit from the list above: ";
    std::string sentence;
    std::getline(std::cin, sentence);

    std::cout << "Would you like to replace a fruit from your sentence? (Yes or No?): ";
    std::string input;
    std::cin >> input;
    
    if (input == "Yes")
    {
        std::string fruitToReplace;
        std::string replacementFruit = "pineapple";
        std::vector<std::string>::const_iterator it;
        do
        {
            std::cout << "Which fruit will you replace? : ";
            std::cin >> fruitToReplace;
            it = std::find(fruits.cbegin(), fruits.cend(), fruitToReplace);
        }
        while (it == fruits.cend());
        
        std::cout << "Replacing " << *it << " by " << replacementFruit << "..." << std::endl;
        
        for (size_t start = 0; (start = sentence.find(fruitToReplace, start)) != std::string::npos; start += replacementFruit.length())
        {
            if (start != std::string::npos)
                sentence.replace(start, fruitToReplace.length(), replacementFruit);
        }
        
        std::cout << "After replacement: " << sentence << std::endl;
    }

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...