Thread: recursion

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    26

    Unhappy recursion

    hi.. I am suppose to write a recursive function that reverses the characters from s[j] to s[k] without changing the following main program:
    Code:
    int main (void)  {
    char phrase [] = "by the sea, by the beautiful sea";
    rev (phrase,3,17);
    printf ("%s\n",phrase);
    return 0;
    }
    and the output is "by eht yb, aes eht beautiful sea"

    I wrote the following program, but i had to change the main function! i dont know how to do it without changing the main! plz HELP!

    Code:
    # include <stdio.h>
    
    void rev (char [],int,int);
    int main ()  {
    char phrase [] = "by the sea, by the beautiful sea";
    rev (phrase,3,17);  /* reverse the characters from phrase [3] to phrase [17] */
    return 0;
    }
    
    void rev (char sen [],int start, int end)
    {
    if (end >= start)
    printf ("%c",sen [end]);
    rev (sen,start,end-1);
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    When looking at your recursive function it seems to me that it never ends, since rev() is always called. To solve that problem you should think about the two basic questions in recursion:

    - when does the recursion stop
    - what is the recursion step

    If you solve these questions, then you have the answer to your problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM