(Exercise) Create - repeatString.cpp In this part of the lab, you will create a
ID: 3912011 • Letter: #
Question
(Exercise) Create - repeatString.cpp In this part of the lab, you will create a function (repeatStr) that concatenates a string by a number of times. There should be two functions in this program: main and repeatStr. The 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 the number of times to repeat string ". 3. Call the function repeatstr. 4. Output "The resulting string is: " and the resulting string 5. Start over again. User can terminate the program by entering -1 (zero) for the number of times The repeatStr function will 1. Take in a string and an int as arguments 2. Concatenate the string argument by the number of times specified by the int argument. 3. Return the resulting string Before starting to write your program, use a test editor to write the pseudocode of BOTH functions and name you file LogicChecke4. You will need to submit the pseudocode in order to receive full credit. Again, there is no unique way to write pseudocode. It will be good enough as long as you can understand it and translate it to C++ code. Ask vour TA if you are not sure about the structure of the pseudocode Sample Run (user input shown in blue): Enter a string: e Enter the number of times to repeat string: 7 The resulting string is: 0000000 Enter a string: bla Enter the number of times to repeat string: 3 The resulting string is: blablabla Enter a string: CSE 30 Enter the number of times to repeat string: 2 The resulting string is: CSE 30CSE 30 Enter a string: abc Enter the number of times to repeat string: eExplanation / Answer
// imports
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
// funciton for repeatStr
string repeatStr(string inp,int n){
//initial result value is empty string
string result = "";
// loop till n is greater than zero
while(n>0){
// add inp to result
result += inp;
// decrement n
n--;
}
// return the result string
return result;
}
int main() {
// declare variables
string inp,result,temp;
int n;
// infinite loop
while(true){
// get input from user
cout << "Enter a string:";
getline(cin,inp);
cout << "Enter the number of times to repeat string:";
//cin >> n;
getline(cin,temp);
// convert string to int
n = stoi(temp);
// if n is zero, break the loop
if(n==0){
break;
}
// call the function.
result = repeatStr(inp,n);
// print output
cout << "The resulting string is: " << result << endl;
}
}
/*
sample output
Enter a string: 0
Enter the number of times to repeat string: 7
The resulting string is: 0000000
Enter a string: bla
Enter the number of times to repeat string: 3
The resulting string is: blablabla
Enter a string: CSE 30
Enter the number of times to repeat string: 2
The resulting string is: CSE 30CSE 30
Enter a string: abc
Enter the number of times to repeat string: 0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.