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 -
#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);
-
}
-
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?
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: - 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.
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.... -
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)
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
....
|
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
|
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...
|
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...
| |
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...
|
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
|
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...
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |