Exercise Create combineStringcpp In this part of the lab yo

(Exercise) Create – combineString.cpp
In this part of the lab, you will create a function (combineStr) that concatenates a stringby a number of times.

There should be two functions in this program: main andcombineStr. T

he descriptions of the functions are as follows.
The main function will

1. Prompt the user to “Enter a string: ”.

2. Prompt the user to “Enter a number of times: ”.

3. Call the function combineStr.

4. Output “The resulting string is: ” and the resulting string.

5. Start over again. User can terminate the program by entering 0 (zero) for the numberof times.

The combineStr function will

1. Takes in a string and an integer as arguments.

2. Concatenates the string argument by a number of times according to the integerargument.

3. Return the resulting string.

Before starting to write your program, use a piece of paper or a text editor to write the
pseudocode of BOTH functions. You will need to submit the pseudocode in order to receivefull credit. Again, there is no unique way to write pseudocode. It will be good enough aslong as you can understand it and translate it to C++ code. Ask your TA if you are notsure about the structure of the pseudocode.

Solution

#include <iostream>

using namespace std;
string combineStr(string s,int n){
string returnStr = \"\";
for(int i=0; i<n; i++){
returnStr = returnStr + s;
}
return returnStr;
}
int main()
{
string s;
int n;
while(true){
cout<<\"Enter a string: \";
cin >> s;
cout<<\"Enter a number of times: \";
cin >> n;
if(n == 0){
break;
}
string result = combineStr(s, n);
cout<<\"The resulting string is: \"<<result<<endl;
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a string: Hello                                                                                                                                                                                                                                                  

Enter a number of times: 3                                                                                                                                                                                                                                             

The resulting string is: HelloHelloHello                                                                                                                                                                                                                               

Enter a string: Hai                                                                                                                                                                                                                                                    

Enter a number of times: 5                                                                                                                                                                                                                                             

The resulting string is: HaiHaiHaiHaiHai                                                                                                                                                                                                                               

Enter a string: 0                                                                                                                                                                                                                                                      

Enter a number of times: 0

(Exercise) Create – combineString.cpp In this part of the lab, you will create a function (combineStr) that concatenates a stringby a number of times. There sho
(Exercise) Create – combineString.cpp In this part of the lab, you will create a function (combineStr) that concatenates a stringby a number of times. There sho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site