473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
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
3 6620
sicarie
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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
1,806 1GB
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

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

Similar topics

4
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart...
7
by: Steven Bethard | last post by:
How do I make sure that my entire string was parsed when I call a pyparsing element's parseString method? Here's a dramatically simplified version of my problem: py> import pyparsing as pp py>...
2
by: flipflop | last post by:
Martin Dickopp <expires-2004-06-30@zero-based.org> wrote in message news:<cunaczsrj0s.fsf@zero-based.org>... > > is there > any particular reason why you cannot or don't want to use the `qsort' >...
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
2
by: peace357 | last post by:
I am trying to write two recursive functions involving two strings, 1)CAT & 2)MAN. My function needs to print out: TACMAN ATCMAN CTAMAN TCAMAN ACTMAN CATMAN
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.