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
Multiple Choice
ra1l [238]
I need more details to your question
4 0
2 years ago
Read 2 more answers
Which term describes a Cloud provider allowing more than one company to share or rent the same server?
svlad2 [7]

Answer:

multitenancy is the term.

6 0
3 years ago
What are the four causes of electrical faults?
Arada [10]

Answer:

Electrical faults are also caused due to human errors such as selecting improper rating of equipment or devices, forgetting metallic or electrical conducting parts after servicing or maintenance, switching the circuit while it is under servicing, etc.

Explanation:

6 0
3 years ago
______________ help protect the lower legs and feet from heat hazards like molten metal and welding sparks.
astraxan [27]

Answer:

leggings

Explanation:

they allow the metal or sparks to not hurt you can the leggings can be easily and quickly removed.

8 0
3 years ago
An irreversible heat pump and a Carnot heat pump operate between the same two thermal reservoirs. Which heat pump has higher COP
Shkiper50 [21]

Answer:

Carnot heat pump

Explanation:

Carnot heat pump is an ideal heat pump in which all processes are reversible and that consume minimum amount of work to and produces maximum amount of heating effect compare to all real engine.And that is why COP of Carnot heat pump is more as compare to real heat pump.

All real heat pump are not perfectly reversible heat pump So this is also called irreversible heat pump .Due to irreversibility the COP of  irreversible heat pump is always  less than the COP of  Carnot heat pump.

6 0
3 years ago
Other questions:
  • Which type of door consists of thin pieces of wood, glass, or louvers placed within a framed rectangular area?
    7·2 answers
  • Which field in a Transmission Control Protocol (TCP) header is chosen from ephemeral ports?
    11·1 answer
  • A flow of 12 m/s passes through a 6 m wide, 2 m deep rectangular channel with a bed slope of 0. 001. If the mean velocity of flo
    12·1 answer
  • Hi all, could you solve this please?<br> What is the value of the resistance R
    14·1 answer
  • Which of the following sentences uses the word malleable correctly?
    7·2 answers
  • . H<br> Kijwhayhwbbwyhwbwbwgwwgbwbwhwh
    6·2 answers
  • 6.3. A __________ is used to indicate the base material that needs to be beveled.
    12·1 answer
  • A student used a 500-ml graduated cylinder to measure the volume of water in a 1-cup measure. three trials of the measurement ga
    10·1 answer
  • Please help me on this it’s due now
    14·1 answer
  • Key term of Bimetal strip
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!