By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

reversing a string using recursive functions

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>//header for string function like strlen
  3.  
  4. void swop (char[ ],int,int);
  5. void reverse (char[ ]);
  6. void rev(char[ ],int, int);
  7.  
  8. void main()
  9. {
  10.     char str[100];//declare a string with maximum length ofa 100
  11.  
  12.     printf("\nPlease enter the string: ");//prompts user for a string
  13.  
  14.     rev(str ,0, strlen(str));
  15.     reverse(str);
  16.  
  17. }
  18.  
  19. void swop (char str1[ ],int first1,int last1)
  20. {
  21.     rev(str1,first1,last1);
  22.     for(int x=first1;x < last1;x++)
  23.     {
  24.         if (str1[x]==first1)
  25.         {
  26.             swop(str1,last1,first1);
  27.             printf("%c",str1[first1]);
  28.         }
  29.  
  30.         else
  31.         {
  32.             swop(str1,last1-1,first1+1);
  33. }
  34.  
  35. void reverse (char str[100])
  36. {
  37.     if(str != '\0')
  38.     {
  39.          reverse(str+1);
  40.  
  41.     printf("%c",str);
  42.     }
  43. }
  44.  
  45. void rev(char str2[] ,int first, int last)
  46. {
  47.     gets(str2);    
  48.         first=0;
  49.         last=strlen(str2);
  50. }
  51.  
Mar 13 '07 #1
Share this Question
Share on Google+
3 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
How is it not working? Are you getting any errors (if so, please copy and paste in here), or just garbage coming back out?
Mar 13 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
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:

Expand|Select|Wrap|Line Numbers
  1. swap(char str[], int from, int to) // Spelled swap, not swop
  2. // Swaps characters position from and to of str
  3.  
  4. reverse(char str[])
  5. // Calls rev with str, the first position, and last position of meaningful data in str
  6.  
  7. rev(char str[], int first, int second)
  8. // Calls swap to swap characters, then calls itself with changed first/last values.
Mar 13 '07 #3

DeMan
100+
P: 1,806
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.....(although 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....

Expand|Select|Wrap|Line Numbers
  1. void rev(char str1[], int first, int last)
  2. {
  3.   if(last > first)
  4.   {
  5.     swap (str1, first, last);
  6.     rev(str1, first+1, last -1);
  7.   }
  8. }
  9.  
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)

}
Mar 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.