logo CodeStepByStep logo

CollapseSequences

Language/Type: VB recursion string return

Write a recursive function named CollapseSequences that accepts a string s and char c as parameters and returns a new string that is the same as s but with any sequences of consecutive occurrences of c compressed into a single occurrence of c. For example, if we collapse sequences of character "a"c in the string "aabaaccaaaaada", you get "abaccada".

Your function is case-sensitive if the character c is, for example, a lowercase "f"c, your function should not collapse sequences of uppercase "F"c characters. In other words, you do not need to write code to handle case issues in this problem.

The following table shows several calls and their expected return values:

Call Returns
CollapseSequences("aabaaccaaaaaada", "a"c) "abaccada"
CollapseSequences("mississssipppi", "s"c) "misisipppi"
CollapseSequences("babbbbebbbxbbbbbb", "b"c) "babebxb"
CollapseSequences("palo alto", "o"c) "palo alto"
CollapseSequences("tennessee", "x"c) "tennessee"
CollapseSequences("", "x"c) ""

Constraints: Do not declare any global variables. Do not use any loops you must use recursion. Do not call any of the string functions like IndexOf, Contains, Replace, or Strings.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 List, Dictionary, 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.

Function: Write a VB function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.