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
Which of the following are the same as 1545.5347
Reptile [31]

Explanation:

D the answer is D and anyone can answer this Question

5 0
3 years ago
Retype the statements, correcting the syntax error in each print statement. print('Predictions are hard.") print(Especially abou
marshall27 [118]
Print(‘Predictions are hard’)
print(‘Especially about the future’)
user_num = 5
print(‘user_num is:’+str(user_num))

There are many ways to do the last print. Look up string interpolation
3 0
3 years ago
How many types of engineers are there
Fiesta28 [93]
Could be more than 10 but

Biomedical engineer
Electrical engineer
Chemical engineer
Mechanical engineer
Computer engineer
Aerospace engineer
Civil engineer
Petroleum engineer
Environmental engineer
Marine engineer

Are just some
6 0
3 years ago
Read 2 more answers
What plane is this? i want the model
myrzilka [38]

Answer:

I think that plane's name is

KLM ....Because you can see the mysterious Leter in the plane's

Forward and the back of the plane

4 0
2 years ago
g Water is being heated in a closed pan on top of a range while being stirred by a paddle wheel. During the process, 30 kJ of he
AVprozaik [17]

Answer:

38kJ

Explanation:

Final Energy= Total Energy at the beginning + Total energy added - energy lost

Final Energy = 12.5 + 500/1000 + 30- 5

                   = 38kJ

8 0
4 years ago
Other questions:
  • Technician A says diesel engines are also called compression ignition engines. Technician B says diesel engines have much higher
    9·1 answer
  • What type of footwear protects your toes from falling objects and being crushed?
    6·2 answers
  • What is the main role of matrix in composites! a)-to transfer stress to the other phases b)- to protect phases from environment
    7·1 answer
  • A circular bar will be subjected to an axial force (P) of 2000 lbf. The bar will be made of material that has a strength (S) of
    14·1 answer
  • PythonA group of statisticians at a local college has asked you to create a set of functionsthat compute the median and mode of
    9·1 answer
  • Find Re,Rc,R1 and R2!? Show your work.(hlp plz)
    7·1 answer
  • A well-established way of power generation involves the utilization of geothermal energy-the energy of hot water that exists nat
    9·1 answer
  • Once you have chosen a topic, what should you do before beginning the research process? a. Find as many possible facts and detai
    13·1 answer
  • 3. Determine the most unfavorable arrangement of the crane loads and
    6·1 answer
  • The enforcement of OSHA standards is provided by federal and state
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!