reversing a string using recursive functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Babikie
    New Member
    • Mar 2007
    • 1

    #1

    reversing a string using recursive functions

    Write a program that performs a reverse recursion with following functions.
    void swop (char[ ],int,int);
    void reverse (char[ ]);
    void rev(char[ ],int, int);
    User should enter a string and all character should be reversed. Note! The
    rev function should be used as the recursive function, accepting the string, plus
    the first and last position of string. The reverse() passes the first and the last
    position of the string to the rev() which performs the recursive operation. The
    swop() accepts the string with the first and the last position of the string.
    e.g. The string "Testing" is reversed to "gnitseT"

    This is what i did, but its not working... Pls help
    Code:
    #include <stdio.h>
    #include<string.h>//header for string function like strlen
    
    void swop (char[ ],int,int);
    void reverse (char[ ]);
    void rev(char[ ],int, int);
    
    void main()
    {
    	char str[100];//declare a string with maximum length ofa 100
    
    	printf("\nPlease enter the string: ");//prompts user for a string
    
    	rev(str ,0, strlen(str));
    	reverse(str);
    
    }
    
    void swop (char str1[ ],int first1,int last1)
    {
    	rev(str1,first1,last1);
    	for(int x=first1;x < last1;x++)
    	{
    		if (str1[x]==first1)
    		{
    			swop(str1,last1,first1);
    			printf("%c",str1[first1]);
    		}
    
    		else
    		{
    			swop(str1,last1-1,first1+1);
    }
    
    void reverse (char str[100])
    {
    	if(str != '\0')
    	{
             reverse(str+1);
     
        printf("%c",str);
    	}
    }
    
    void rev(char str2[] ,int first, int last)
    {
    	gets(str2);	
    		first=0;
    		last=strlen(str2);
    }
    Last edited by RedSon; Mar 13 '07, 06:34 PM. Reason: CODE tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    How is it not working? Are you getting any errors (if so, please copy and paste in here), or just garbage coming back out?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      You have implemented the functions incorrectly. rev() is supposed to be the function doing the most work, not swop(). As of right now, you merely use rev() to initialize your array and assign some arbitrary values to first and last - which, since they are local variables, will 'die' after the function is called.

      Each function should do the following:

      Code:
      swap(char str[], int from, int to) // Spelled swap, not swop
      // Swaps characters position from and to of str
      
      reverse(char str[])
      // Calls rev with str, the first position, and last position of meaningful data in str
      
      rev(char str[], int first, int second)
      // Calls swap to swap characters, then calls itself with changed first/last values.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        The way I read the problem, the reverse() function is the driver, calling rev() to do this actual reverse which uses the swop() (sic) function, to swop first an last letters.....(al though correct me If I'm wrong, because the swop function is confusing me a little)

        This means
        a) you do not want gets inside rev() (and rev is supposed to be recursive)
        b) your reverse function seems to do nothing but count the number of letters in the string
        c) your swop appears to be where you did your recursion.
        d) I'm not sure you can cap an input like you have on the reverse function.

        Since we explicitly pass first and last, we can pass the entire array through the recursive function, altering only these values (and the swapped values in the array). The logic for the rev function is shown below (in pseudocodish sort of a way)....I leave it to you to implement the swap and the calling function, and to turn this function into working code....

        Code:
        void rev(char str1[], int first, int last)
        {
          if(last > first)
          {
            swap (str1, first, last);
            rev(str1, first+1, last -1);
          }
        }
        I'm fairly sure that as a pointer is passed to the function, this will modify your original array (and you could make the array you pass smaller each time, but it would be more effort for no saving, since you only pass a reference)

        }

        Comment

        Working...