logo CodeStepByStep logo

replaceAll

Language/Type: C++ recursion string return
Related Links:
Author: Marty Stepp (on 2016/06/16)

Write a recursive function named replaceAll that accepts three parameters: a string s, a char from, and a char to as parameters, and returns a new string that is the same as s but with any occurrences of from changed to to. For example, the call of replaceAll("crazy raccoons", 'c', 'k') should return "krazy rakkoons" and the call of replaceAll("BANANA", 'A', 'O') should return "BONONO".

Your function is case-sensitive; if the character from is, for example, a lowercase 'f', your function should not replace uppercase 'F' characters. In other words, you should not need to write code to handle case issues in this problem.

Constraints:

  • Do not use any loops; you must use recursion.
  • Do not declare any global variables.
  • Do not call any of the following string member functions: find, rfind, indexOf, contains, replace, split. (The point of this problem is to solve it recursively; do not use a library function to get around recursion.)
  • Do not use any auxiliary data structures like Vector, Map, Set, array, etc.
  • You can declare as many primitive variables like ints as you like, as well as strings.
  • You are allowed to define other "helper" functions if you like; they are subject to these same constraints.
Type your C++ solution code here:


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.