C++ Write a recursive function that takes in a 2 character string and a number o
ID: 665095 • Letter: C
Question
C++
Write a recursive function that takes in a 2 character string and a number of times to repeat
the pattern. The function should return a string that has the rst character repeated the
speci ed number of times followed by the second character repeated the speci ed number of
times. The function declaration should be as follows:
std::string createPattern(string pattern, int numRepeats);
Hint: You can index into a string using [] or substring. You can concatenate strings using
+.
For example, if you call createPattern(()", 4), it should return (((())))".
Explanation / Answer
function repeatRecursive(string:String, numTimes:uint):String { if(numTimes == 0) return ""; if(numTimes & 1) return string + repeatRecursive(string, numTimes - 1); var tmp:String = repeatRecursive(string, numTimes/2); return tmp + tmp; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.