473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reversing a string using recursive functions

2 New Member
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 6657
sicarie
4,677 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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 Top Contributor
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....

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
5975
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 in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
7
2215
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> match = pp.Word(pp.nums) py> def parse_num(s, loc, toks): .... n, = toks .... return int(n) + 10 ....
2
4263
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' > function in the standard C library? I'm using gcc on mingw and assumed it was so minimal it wouldn't include it. Should've checked because it does of course. > As to your question, just invert all comparisons of the elements to be
11
8095
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 a backcolor/forecolor/etc property and calculate the "reverse colour"? In other words, If a user picks 255 (red) for a control backcolor, I'd like to be able to calculate the opposite or negative of that colour and assign the control's...
17
2831
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 creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
13
3174
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 creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
8
4764
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. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
3
6104
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 form with two textboxes on it, on positioned above the other. As each character is entered into the upper textbox, the string that has been entered into the upper textbox must appear in the lower textbox, but in reverse. If the input field contains...
2
3005
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
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.