473,388 Members | 1,493 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,388 software developers and data experts.

Problem with recursion

I'm trying to do a function that returns a string which is a string the
characters in inverse order of the one given as a parameter. It
compiles but it crashes! Can anyone help me?

string inverse(string s, int pos, int size)
{
if(pos==size-1)
return s;
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
}
}

Mar 26 '06 #1
4 1667

Gaijinco wrote:
I'm trying to do a function that returns a string which is a string the
characters in inverse order of the one given as a parameter. It
compiles but it crashes! Can anyone help me?

string inverse(string s, int pos, int size)
{
if(pos==size-1)
Better:

if(pos >= size)
return s;
return s;
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
This probably should be:

inverse(s, pos + 1, size - 2);
}
}


Mar 26 '06 #2

Gaijinco wrote:
I'm trying to do a function that returns a string which is a string the
characters in inverse order of the one given as a parameter. It
compiles but it crashes! Can anyone help me?

string inverse(string s, int pos, int size)


Your implementation make far too many copies, and the contract is
unclear. If I had to guess, I'd say it returns a string which is a copy
of the characters s[pos]..s[pos+size] reversed. Is this what you mean?

Mar 27 '06 #3
Yeah, if you input carlos it would output solrac but there is something
quite odd, this works fine:

void inverse(string s, int pos, int size)
{
if(pos==size-1)
{
cout << s;
return s;
}
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
}
}

But if I try to return the string, then the program crashes. I'm not
sure why is that!

Mar 27 '06 #4

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Yeah, if you input carlos it would output solrac but there is something
quite odd, this works fine:

void inverse(string s, int pos, int size)
{
if(pos==size-1)
{
cout << s;
return s;
}
else
{
int idx = s.size()-1;
char character = s.at(idx);
s.erase(idx);
s.insert(s.begin()+pos,character);
inverse(s,pos+1,size);
}
}

But if I try to return the string, then the program crashes. I'm not
sure why is that!


You defined the function as void, but then have a "return s" statement in
the first part.

If it is supposed to return a string, then define it to return a string.
And then you also need a return statement for the else condition. (Probably
"return inverse(s,pos+1,size):"? I haven't tested the thought.)

If it is not supposed to return a string, then it should be operating on s
directly, and never returning a value. In that case, make s a string&
instead, and change "return s;" to just "return;".

-Howard
Mar 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
19
by: snowdy | last post by:
I am using Interactive C with my handboard (68HC11) development system but I've got a problem that I am asking for help with. I am not new to C but learning all the time and I just cant see how to...
16
by: makko | last post by:
Hello, anyone know how to writre a program that take a commandline formula and prints the calculated result? example; $program 1+(2x3(3/2))-8 reagrds; Makkko
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
1
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
24
by: proctor | last post by:
hello, i have a small function which mimics binary counting. it runs fine as long as the input is not too long, but if i give it input longer than 8 characters it gives RuntimeError: maximum...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.