1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Elina [12.6K]
3 years ago
8

Write a function named replaceSubstring. The function should accept three C-string arguments, let's call them string1, string2,

and string3. It should search string1 for all occurrences of string2. When it finds an occurrence of string2, it should replace it with string3. For example, suppose the three arguments have the following values: string1 : the dog jumped over the fence string2: the string3: that Using these values for the three arguments, the function would return another C-string object with the value "that dog jumped over that fence". Please do not use any of the functions in the string class. However you may use any functions such as strstr and strncpy.

Computers and Technology
2 answers:
blsea [12.9K]3 years ago
7 0

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.

hoa [83]3 years ago
6 0

Answer:

All is in explanation.

Explanation:

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

//function prototype

string replaceSubstring(const char *, const char *, const char*);

string replaceSubstring(string, string, string);

int main()

{

//declare three char array to hold strings of max 100 chars

char string1[101];

char string2[101];

char string3[101];

//prompt user to enter input

//then read

cout << "Enter string to search:\n";

cin.getline(string1, 101);

cout << "Enter the string you want to replace:\n";

cin.getline(string2, 101);

cout << "What do you want to replace it with?\n";

cin.getline(string3, 101);

cout << "Your updated string is:\n";

cout << replaceSubstring(string1, string2, string3);

//return 0 to mark successful termination

return 0;

}

string replaceSubstring(const char *st1, const char *st2, const char *st3){

//declare pointers start and occurrence

//initialize start with string to search

//char *startSearch = st1;

//initialize occurrence with the first occurrence

//of st2 in st1

char *occurrence = strstr(st1, st2);

//declare empty string

string output = "";

//using occurrence as control for while loop

//means that it will iterate until no more

//occurrences are found

while(occurrence){

//append to final string the characters of st1

//up until first occurrence of st2

output.append(st1, (occurrence-st1));

//append st3 to final string

output.append(st3, strlen(st3));

//update occurrence to point to character

//of st1 just after first occurrence of st2

st1 = st1 + (occurrence-st1) + strlen(st2);

//find new occurrence

occurrence = strstr(st1, st2);

}

//st1 now points to first character after

//last occurrence of st2

output.append(st1, strlen(st1));

return output;

}

string replaceSubstring(string st1, string st2, string st3){

//convert input strings to C-strings

const char *ptr1 = st1.c_str();

const char *ptr2 = st2.c_str();

const char *ptr3 = st3.c_str();

//call function that accepts C-strings

replaceSubstring(ptr1, ptr2, ptr3);

}

You might be interested in
Algunos de los navegadores que existen son:
AlekseyPX

Answer:

Un navegador que no existe es firechicken.  ja ja  

A browser that does not exist is firechicken.   lol

Explanation:

Un navegador que no existe es firechicken.

¡Espero que esto ayude! :)

¡Marque el más inteligente!

A browser that does not exist is firechicken.

Hope this helps! :)

Please mark brainliest!

5 0
2 years ago
How do i answer a qestion on brainly
viva [34]

Answer:

Click on an answer, then click "Add Answer +(?)"

Explanation:

This way you'll be able to add a question to any brainly question that you are able to add a question to. Such as this one. You will receive half the points that the person asking the question assigned to the question, with the minimum being 10 points. After this you will receive those points and be able to write a response much like this one allowing you and 1 other person to answer the same question and received thanks and or brainliest by which the user asking the question can give out to the person who they believe gave the best answer.

Hope this helps! <3

5 0
2 years ago
Array Challenge Have the function ArrayChallenge (arr) take the array of numbers stored in arr and return the string true if any
vampirchik [111]

Answer:

The code is given as follows,

Explanation:

Code:

#include <stdio.h>

#include <string.h>  

int n; //to store size of array  

char* ArrayChallenge(int arr[]) //function returns string true or false

{

  int i, j;

  int sum = 0;

  for(i = 0; i < n; i++)

  {

      sum = sum + arr[i]; //count sum

  }  

  for(i = 0; i < n; i++)

  {

      for(j = i+1; j < n; j++) //loop for every two elements in array

      {

          if(arr[i]*arr[j] > 2*sum) //check if proudct of two elements > 2 times sum

          {

              printf("\n%d x %d = %d, 2xSum = %d\n", arr[i], arr[j], arr[i]*arr[j], 2*sum);

              return "true"; //If proudct of two elements > 2 times sum. return true

          }

      }

  }

  return "false"; // If proudct of two elements < 2 times sum. return false

}  

int main()

{  

  printf("\nEnter size of array: ");

  scanf("%d", &n); //read size of array

  int A[n]; //array of size n

  printf("\nEnter array elements: ");

  int i;

  for(i = 0; i < n; i++)

  {

      scanf("%d", &A[i]); //read array from stdin

  }

  printf("%s\n",ArrayChallenge(A)); //ccall function and print answer

 

  return 0;

}

6 0
3 years ago
To make a drop shadow larger what should you change about the drop shadow in gimp
vlabodo [156]
To Change the Drop Shadow Just Go to effects on the photo go to drop shadow than change the spread and Distinse and Opasity.
4 0
3 years ago
You want to get an average of the number of siblings people in a group have. Which data format would be best for you to receive?
never [62]

The data format would be best for you to receive is Spreadsheet.

<h3>What is spreadsheet?</h3>

A spreadsheet is a computer program that allows you to compute, organize, analyze, and store data in tabular form. Spreadsheets were created as electronic counterparts to paper accounting spreadsheets. The software runs on data entered into table cells. Each cell can include numeric or text data, as well as the results of formulas that calculate and display a value dependent on the contents of other cells. One such electronic document may also be referred to as a spreadsheet.

Spreadsheet users can change any recorded value and see how it affects calculated numbers. Because of this, the spreadsheet is ideal for "what-if" analysis because many situations may be studied quickly without human recalculation. Modern spreadsheet software can include numerous interacting sheets and display data as text and numbers or as graphics.

To learn more about spreadsheet visit:

brainly.com/question/8284022

#SPJ4

6 0
2 years ago
Other questions:
  • which kind of device does a computer need in order to provide information to a person or something else
    7·1 answer
  • Role-playing, action, educational, and simulations are examples of computer and video game _____. Windows and Apple offer_____ ,
    13·1 answer
  • You are most likely to use<br> images for photos or digital paintings.
    6·1 answer
  • How many responses does a computer expect to receive when it broadcasts an ARP request?why?
    12·1 answer
  • The program is to be answered in Java Programming not C++ Please answer the following: Modify the BarChart program to accept the
    6·1 answer
  • The building blocks of coded language are called
    11·2 answers
  • Please help ASAP!
    15·1 answer
  • Kyle returns to work the next day and he would like to continue working on the document from yesterday. What should Kyle do?
    7·1 answer
  • Compare and contrats the vain digestive system from the human digestive system.​
    10·1 answer
  • 4.3 Code Practice: Question 1
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!