I’m trying to write a recursive function that would get some string, as well as the lenght of that string as its parameters, and then print out the original string, as well as the reverse order of the vowels in that string. For example if the string is ‘Horse’, then the output would be ‘Horse eo’.
What I’m having trouble with is how to get the original string printed while still getting the vowels out in a reverse order. I’m writing this function with a pseudocode, and how I’d print out only the reversed vowels would be as following.
MODULE VowelRecursion(String, n)
IF n != 0 THEN
letter := first letter of String
vowel := ""
IF letter == vowel THEN
vowel := letter
ENDIF
VowelRecursion(remainder of String, n-1)
Print(vowel)
ENDIF
ENDMODULE
Like I mentioned, the problem I have is that I can’t figure out how to get the original string printed after the vowel finding has been done, as the original string needs to be printed first, and to do that wouldn’t it have to be returned first after n gets to 0? The problem with that is that since we’re calling the function with remainder of string, that would be just an empty string when n == 0, right?
As this is a problem I need to solve for school, I’m not looking for any ready made solutions, but I’d like to hear where my thought process is going wrong and what sort of methods I could use to achieve what’s needed.
Thank you.
You may print
letterbefore descending into the next recursion level, i.e. right before the call toVowelRecursion(remainder of String, n-1).