Question

10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string ob

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have solved Question 10. replaceString()

Step 1 : Save the following code in Replace.cpp

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string replaceString(string string1, string string2, string string3) {
string result;
// Create a string stream for parsing
stringstream stream(string1);
string temp;
// Tokenizing w.r.t. space ' '
while (getline(stream, temp, ' ')) {
if (string2 == temp) {
result += string3;
} else {
result += temp;
}
result += ' ';
}
return result;
}

int main() {
string string1 = "the dog jumped over the fence";
string string2 = "the";
string string3 = "that";
string replaced = replaceString(string1, string2, string3);
cout << replaced;
return 0;
}

Step 2 : Compile the program and run. It will produce the following output

that dog jumped over that fence

Note. Please file a different question for Question 11 since I do not have answer for this. Some one else will answer

Add a comment
Know the answer?
Add Answer to:
10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this done in C++ No global variables No labels or go-to statements No infinite loops, examples include: for(;;...

    I need this done in C++ No global variables No labels or go-to statements No infinite loops, examples include: for(;;) while(1) while(true) do{//code}while(1); No break statements to exit loops Write a program with three functions: upper, lower, and reverse. The upper function should accept a pointer to a C-string as an argument. It should step through each character in the string, converting it to uppercase. The lower function, too, should accept a pointer to a C-string as an argument. It...

  • From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a...

    From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.                                           Data:   “This is a test string for string length” Prt String Backwards: Write a Function that passes in a C-String and prints the string backwards.      Data: “This is a test string for string backwards” replaceSubstring: Write a Function that accepts three C-Strings – str1,...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • In the space below, write a C function definition for a function named StrLower, which will...

    In the space below, write a C function definition for a function named StrLower, which will receive one parameter, a pointer to a null-terminated string (i.e., pointer to char), convert each character in the string to lowercase (using the tolower function from <ctype.h>), and return nothing to the caller. Inside the function definition, you may use a for loop or a while loop. If you use any variables other than the incoming parameter, you must define them locally in the...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments:...

    c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Assembly Programming Question. Read keystroke, reverse case and display, stop on ESC. Also relevant: Also relevant:...

    Assembly Programming Question. Read keystroke, reverse case and display, stop on ESC. Also relevant: Also relevant: Also relevant (though you cant use Irvine16.inc): Assembly Programming Question. Read keystroke, reverse case and display, stop on ESC. Assembly Programming Question. Read keystroke, reverse case and display, stop on ESO. 3. Write a program that reads a user keystroke and if it?s an alphabetical character, reverses it case then displays it. The program should stop when the user hits the ESC key (1Bh)....

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT