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
What is the maximal coefficient of performance of a refrigerator which cools down 10 kg of water (and then ice) to -6C. Upper he
inysia [295]

Given:

Temperature of water, T_{1} = -6^{\circ}C =273 +(-6) =267 K

Temperature surrounding refrigerator, T_{2} = 21^{\circ}C =273 + 21 =294 K

Specific heat given for water, C_{w} = 4.19 KJ/kg/K

Specific heat given for ice, C_{ice} = 2.1 KJ/kg/K

Latent heat of fusion,  L_{fusion} = 335KJ/kg

Solution:

Coefficient of Performance (COP) for refrigerator is given by:

Max COP_{refrigerator} = \frac{T_{2}}{T_{2} - T_{1}}

= \frac{267}{294 - 267} = 9.89

Coefficient of Performance (COP) for heat pump is given by:

Max COP_{heat pump} = \frac{T_{1}}{T_{2} - T_{1}}\frac{294}{294 - 267} = 10.89

6 0
3 years ago
. In one stroke of a reciprocating compressor, helium is isothermally and reversibly compressed in a piston + cylinder from 298
andriy [413]

Answer:

5.7058kj/mole

Explanation:

Please see attachment for step by step guide

5 0
3 years ago
3. Which of these instruments is used to measure wind speed? A. anemometer C. wind sock B. thermometer D. wind vane It is an ins
siniylev [52]

Answer:

wind vane if it can be used to show wind speed and the other is a

Explanation:

please mark 5 star if im right and brainly when ya can

5 0
3 years ago
In C++ the declaration of floating point variables starts with the type name float or double, followed by the name of the variab
zubka84 [21]

Answer:

The given grammar is :

S = T V ;

V = C X

X = , V | ε

T = float | double

C = z | w

1.

Nullable variables are the variables which generate ε ( epsilon ) after one or more steps.

From the given grammar,

Nullable variable is X as it generates ε ( epsilon ) in the production rule : X -> ε.

No other variables generate variable X or ε.

So, only variable X is nullable.

2.

First of nullable variable X is First (X ) = , and ε (epsilon).

L.H.S.

The first of other varibles are :

First (S) = {float, double }

First (T) = {float, double }

First (V) = {z, w}

First (C) = {z, w}

R.H.S.

First (T V ; ) = {float, double }

First ( C X ) = {z, w}

First (, V) = ,

First ( ε ) = ε

First (float) = float

First (double) = double

First (z) = z

First (w) = w

3.

Follow of nullable variable X is Follow (V).

Follow (S) = $

Follow (T) = {z, w}

Follow (V) = ;

Follow (X) = Follow (V) = ;

Follow (C) = , and ;

Explanation:

4 0
3 years ago
Help please really fast!!
ra1l [238]

Answer:368 hdhtygtÿ

901 vuiøöńč

Explanation:

6 0
3 years ago
Other questions:
  • A decorative fountain was built so that water will rise to a hieght of 8 feet above the exit of the pipe. the pipe is 3/4 diamet
    5·1 answer
  • If a pendulum takes 2 sec to swing in each direction, find the period and the frequency of the swing
    15·1 answer
  • Suppose a student rubs a Teflon rod with wool and then briefly touches it to an initially neutral aluminum rod suspended by insu
    6·1 answer
  • Consider a 0.15-mm-diameter air bubble in a liquid. Determine the pressure difference between the inside and outside of the air
    10·1 answer
  • A moving-coil instrument, which gives full-scale deflection with 0.015 A has a copper coil having resistance of 1.5 Ohm at 15°C
    7·1 answer
  • What can be used to relieve stress in a weld.
    12·2 answers
  • In multi-grade oil what is W means?
    11·1 answer
  • 3. What is special about beryllium-copper alloy tools?
    6·2 answers
  • Describe two fundamental reasons why flexural strength should depend on porosity
    14·1 answer
  • A high compression ratio may result in;
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!