SparkNotes Shopping Cart  |     |  Checkout
Brought to you by Barnes and Noble
Examples of Recursion
  
 
Problems
Problem 1.1: Write the reverse() function recursively. This function takes a string and the length of the string as arguments and returns the same string with its characters in the reverse order. [Solution]
Problem 1.2: Challenge: You're given a piece of memory containing characters divided into two sections, section a and section b, that follow each other in memory, b after a. You are also supplied with the lengths of a and b. Write a function that uses your reverse() function from above to swap the two sections such that the a section follows the b section. This does not require recursion. [Solution]
Problem 1.3: Write a function count_spaces(char *s) that counts the number of whitespace characters that appear in a string. Characters are whitespace as defined by the isspace() function in the ctype library. Write this function recursively. [Solution]
Problem 1.4: Why would a programmer most likely not use recursion for an implementation of the string library? [Solution]
Problem 1.5: Write the function strrchr() iteratively and recursively. [Solution]
Problem 1.6: A palindrome is a sequence of characters or numbers that looks the same forwards and backwards. For example, "Madam, I'm Adam" is a palindrome because it is spelled the same reading it from front to back as from back to front. The number 12321 is a numerical palindrome. Write a function that takes a string and its length as arguments and recursively determines whether the string is a palindrome: int ispalindrome(char *s, int len); [Solution]
Problem 1.7: Write a recursive function void replace(char *s, char from, char to); that changes all occurrences of from in s to to. For example, if s were "steve", and from == 'e' and to == 'a', s would become "stava". [Solution]
Help | Feedback | Make a request | Report an error | Send to a friend
 
Beat the SAT II Math IC test with the latest book from the experts at SparkNotes.
More...
 
For students sick of scribbling geometry concepts on index cards, SparkNotes Study Cards are the answer.
More...
 
 
Go to top