Answer:
Here is the C++ program
#include <iostream> // for input output functions
#include <cstring> //functions for c string manipulation
#include <string>// to manipulate arrays of characters
using namespace std; // to identify objects such as cout cin
string replaceSubstring( char *, char *, char*);
// function to replace a substring within a sentence to another string
int main() //start of main() function body
{ char string1[100]; // character type array that holds a string for //example the dog jumped over the fence
char string2[100]; //character type array that holds a sub string of //string1 for example "the"
char string3[100]; //character type array that contains the string that is to //be replaced with the string2
cout << "string 1: "; //prompts user to input string 1
cin.getline(string1, 100); // reads the string entered by the user
cout << "string 2: ";//prompts user to enter string 2
cin.getline(string2, 100); // reads the string entered by the user
cout << "string 3: ";//prompts user to enter string 3
cin.getline(string3, 100); // reads the string entered by the user
cout <<endl; // to insert a new line
cout << replaceSubstring(string1, string2, string3); }
//calls replaceSubstring() function
string replaceSubstring( char *stringA, char *stringB, char *stringC){
char *find = stringA; //pointer find which searches the string in stringA
char *word = strstr(stringA, stringB); //pointer word that locates the //string specified in stringB in stringA
string result = ""; //an empty string to return the final output string
while(word){ // loop searches the given word that is to be replaced
//the loop iterates until no more occurrences of the word are found
//append characters of stringA to result string until the first occurance of //the word specified in stringB
result.append(stringA, (word - stringA));
//append stringC to the result string
result.append(stringC, strlen(stringC));
//update word to point to character of stringA after first occurrence of //stringB
stringA = stringA + (word - stringA) + strlen(stringB);
//find or locate the new occurrence of word
word = strstr(stringA, stringB); }
//stringA points to first character after last occurrence of stringB
result.append(stringA, strlen(stringA));
return result; } //returns the final string in output
Explanation:
In this program there are three arrays.
string1 which holds the string for example the do jumped over the fence.
string2 holds the string whose occurrence is to be found in string1 example, "the" in string2 is the word that is to be located in string1.
string3 contains the string which is used to replace the string specified in the string2. For example value of string3 is "that". So in string1 "the dog jumped over the fence", the word "the" specified in string2 is to be replaced by "that" in string3. So the resultant string will be "that dog jumped over that fence".
After taking the values of above mentioned character arrays from the user, the program calls function replaceSubstring()
The function replaceSubstring() contains a pointer find that is initialized to stringA which is basically string1 to start the search. This function also has a pointer word that is initialized to the first occurrence of stringB (which is basically string2) in stringA (string1).
As an example, the word "the" in stringB is located in stringA (the dog jumped over the fence).
So the while loop is used in which the occurrence of word "the" is found in stringA and when it finds the occurance of stringB (the) in stringA (the dog jumped over the fence) it replaces the value of stringB to the value specified in stringC . The while loop keeps locating the occurances of the stringB in stringA.
The result variable is used to store the final output string.
The append() function is used to first append the value of stringA to result string till first occurance of stringB. It then appends stringC to result string. Then stringA points to first character after the last occurrence of stringB.
The result variable the displays the final output that is : that dog jumped over that fence.
The screen shot of the program along with its output is attached.