C++ MyProgrammingLab problem: Define two functions void appendAll(vector & v, ch
ID: 3562593 • Letter: C
Question
C++ MyProgrammingLab problem:
Define two functions
void appendAll(vector & v, char c) ;
and
void appendAllHelper(vector & v, char c, int start) ;
The first function appends the character c to the end of all strings in v. The second function does the same thing but only for the strings at indexes start and above. To implement appendAll(v,c) you just need to call appendAllHelper(v,c,0).
appendAllHelper() should use recursion (no loops).
Example: If v contains the strings "cat", "hat" and "rat" then appendAll(v,'s') will change the strings to "cats", "hats" and "rats".
DO NOT create any other functions for this problem and DO NOT write the entire program.
Explanation / Answer
//Define two functions
void appendAll(vector<string>& v, char c)
{
appendAllHelper(v,c,0);
}
void appendAllHelper(vector<string>& v, char c, int start)
{
for(int i=start; i<v.size(); i++)
{
v[i] = v[i].append(1,c);
}
}
// The first function appends the character c to the end of all strings in v. The second function does the same thing but only for the strings
// at indexes start and above. To implement appendAll(v,c) you just need to call appendAllHelper(v,c,0).
// appendAllHelper() should use recursion (no loops).
// Example: If v contains the strings "cat", "hat" and "rat" then appendAll(v,'s') will change the strings to "cats", "hats" and "rats".
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.