Question

In: Computer Science

how to write a c++ program that replaces certain words with two "**" input: I went...

how to write a c++ program that replaces certain words with two "**"

input: I went to the store

To buy some fruit

Apples are red

I like oranges better

I saw grapes are green

apples are red, oranges, grapes are green (sub strings that need to be replaced)

iterate through the string input and get an output that replaces the sub strings with **

If the sub strings are not in the input just return the string as it is

Just replace what is in the string if it matches the sub string so if it says in the string I saw grapes are green and the sub string that needs replacing is grapes are green just replace grapes are green. If it just says grapes in the sentence do not replace it.

Solutions

Expert Solution


#include <iostream>
#include <string>

void findReplace(std::string & input, std::string subStr, std::string replaceWith)
{
   // Get the first occurrence of the subString
   size_t firstPosition = input.find(subStr);

   // Repeat using while loop till end is reached
   while( firstPosition != std::string::npos)
   {
       // Replace the occurrence of subString
       input.replace(firstPosition, subStr.size(), replaceWith);
       // Get the next occurrence of the string after the current position
       firstPosition =input.find(subStr, firstPosition + replaceWith.size());
   }
}

int main()
{
//Initialize the string
   std::string input = "I went to the store To buy some fruit Apples are red I like oranges better I saw grapes are green";

//Print the string
   std::cout<<input<<std::endl;

//Call findReplace and replace the subString
   findReplace(input, "Apples are red", "**");
   findReplace(input, "oranges", "**");
   findReplace(input, "grapes are green", "**");

//Print the new string
   std::cout<<input<<std::endl;


   return 0;
}


Related Solutions

c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. Ex: If the input is: 3 automobile car manufacturer maker children kids 15 The automobile manufacturer recommends car seats for children...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program in python that corrects misspelled words. The input for the program is either...
Write a program in python that corrects misspelled words. The input for the program is either a string or a list of strings. The output should be a string or a list depending on the input and should have the spellings corrected. The speller should be able to correct at least the words listed below. You are free to use any data structures we have covered so far including lists and dictionaries. Your program should be in autocorrect.py. Here is...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT