Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ Write a program to prompt the user to enter a line of text and replace

ID: 3818916 • Letter: U

Question

Using C++

Write a program to prompt the user to enter a line of text and replace all “is” with “was”. You DON’T have to test whether “is” is one word or part of another word like “This”. Display the updated string on the screen. (15 points)

HINT: Read a line from the user. In a loop, find the location of “is”, remove “is” from the string and then insert “was”. Think how you need to terminate the loop. Use C++ string functions. Function remove is not available in our C++ version, so use function erase instead. It has the same syntax.

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{
string s;
cout<<"Enter the string: ";
getline(cin, s);
while(s.find("is") != -1){
int pos = s.find("is");
s.erase (pos,2);
s.insert(pos, "was");

}
cout<<s<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter the string: this is suresh. Testing is done                                                                                                                                                                                                                        

thwas was suresh. Testing was done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote