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
Leviafan [203]
3 years ago
13

13. Write a function which is passed two strings. The function creates a new string from the two original strings by copying one

character from each in turn until all characters have been copied.

Engineering
1 answer:
attashe74 [19]3 years ago
3 0

Answer:

I am writing the code in C++. Let me know if you want the program in some other programming language.

#include <iostream>  // includes header file for input output functions

using namespace std;     //to identify objects like cin cout

string CopyStrings(string string1, string string2)  

{   string newString = "";    

   for (int loop = 0; loop < string1.length() ||  

                   loop < string2.length(); loop++)      {      

       if (loop < string1.length())  

           newString += string1[loop];          

       if (loop < string2.length())  

           newString += string2[loop];      }  

   return newString;   }  

int main()  

{   string stringA = "ace";  

   string stringB = "bdf";  

   cout << CopyStrings(stringA, stringB);   }

Output:

abcdef

Explanation:

The function CopyStrings() function takes two strings i.e. string1 and string2 as parameters to copy characters from both the string one character from each.

The newString variable stores the new string after copying characters from both strings string1 and string2.

Then the for loop starts which has a variable loop which is an index variable that traverses through both the strings stored in string1 and string2. The loop continues to execute until it moves through entire length of string1 and string2 which means it copies all the characters from both string1 and string2. length() is used here which returns length of the string1 and string2.

If statement in the for loop checks the character that loop (index) variable is pointing to is less than the string1 length which means it checks each character stored in string1. For example if string1 contains "ace" and loop variable is moving through the string and is currently at "a" then this condition is true. If the condition evaluates to true then the body of if statement is executed. The next statement stores that character a into the newString variable.

Next If statement checks character that loop variable is pointing to is less than the string2 length which means it checks each character stored in string2. For example if string2 contains "bdf" and loop variable is moving through the string and is currently at "b" then this condition is true. If the condition evaluates to true then the body of if statement is executed. The next statement stores that character b into the newString variable.

Then the second iteration starts which again first stores the next character i.e. c from string1 into newString and then stores next character i.e d from string2 into newString.

Then the third iteration starts which again first stores the next character i.e. e from string1 into newString and then stores next character i.e f from string2 into newString.

Then the loop breaks as the loop variable reaches end of both the string1 and string2.

return newString will return the copied string into the output screen which is abcdef.

The screenshot of code along with output is attached.

You might be interested in
Motorcycles are extremely hard to see if they are _______. powered by quiet motors approaching from the side driving on the shou
Assoli18 [71]

Answer: uve earned 5 b point for helping us

Explanation:well know I'm not helping u

7 0
3 years ago
How many steps is a player allowed to take after catching a ball while running
KATRIN_1 [288]

Answer:

A moving player may only take 2 legal steps after catching the ball to shoot, pass or to come to a stop.

so (B. 2)

Explanation:

On this play, the offensive player catches the ball in the air and lands with a left, right, left prior to shooting. This is a traveling violation for taking 3 steps.

7 0
3 years ago
Find the velocity and acceleration of box B when point A moves vertically 1 m/s and it is 5 m
Goshia [24]

Answer:

hshbdhehdjsbdjdissasoe

7 0
4 years ago
g An analog voice signal, sampled at the rate of 8 kHz (8000 samples/second), is to be transmitted by using binary frequency shi
slamgirl [31]

Answer:

The module is why it’s goin to work

Explanation:

4 0
3 years ago
The basic concept of feedback control is that an error must exist before some corrective action can be made?
weqwewe [10]

Answer:

The correct answer is True.

Explanation:

The feedback control system implies that to make a feedback, there must first be an error, otherwise there will be nothing to correct.

This system works so that there is an output that is controlled through a signal.

This signal will be feedback and it will signal an error which will be detected by a controller that will allow entry into the system.

In basic words, this system processes signals, samples them in the form of an output, and re-enters them feedback to detect the error signal.  

7 0
3 years ago
Other questions:
  • Estimate the magnitude of the force, in lbf, exerted on a 12-lb goose in a collision of duration 10^-3s with an airplane taking
    15·1 answer
  • The Canadair CL-215T amphibious aircraft is specially designed to fight fires. It is the only production aircraft that can scoop
    7·1 answer
  • A "scale" is constructed with a 4-ft-long cord and the 10-lb block D. The cord is fixed to a pin at A and passes over two small
    14·1 answer
  • Catch of the day:
    9·2 answers
  • In no less than one hundred fifty words, explain how microstructures reduce friction within fluid power systems. Mention some of
    8·1 answer
  • Which type of material is known as elastomers to materials scientists and engineers
    14·1 answer
  • What are the functions of the peripheral nervous system
    6·2 answers
  • What is the horsepower of a 302 engine
    10·2 answers
  • How do you know what year a mustang is?
    6·1 answer
  • Question 9
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!