Recursive function not fully working

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Recursive function not fully working

 
0
  #1
Sep 5th, 2008
The recursive function is only reversing the first and last characters of a string. I've been trying for hours but cant find the error. Any help would be awesome. The code:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void str_reverse(char str[], int length)
  6. {
  7.  
  8. char temp, temp1;
  9. int starti=0;
  10. int endi = length-starti;
  11. temp1 = str[endi-1];
  12. temp = str[starti];
  13.  
  14. if(starti>=length-starti)
  15. {
  16. return;
  17. }
  18. str[starti] = temp1;
  19. str[endi-1] = temp;
  20. starti++;
  21. return (str_reverse(str, length-1));
  22. }
  23.  
  24. int main()
  25. {
  26. string test= "lighter";
  27. str_reverse(test, 7);
  28. cout << "output= " << test;
  29. return 0;
  30. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Recursive function not fully working

 
0
  #2
Sep 5th, 2008
Originally Posted by nizbit View Post
The recursive function is only reversing the first and last characters of a string. I've been trying for hours but cant find the error. Any help would be awesome. The code:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void str_reverse(char str[], int length)
  6. {
  7.  
  8. char temp, temp1;
  9. int starti=0;
  10. int endi = length-starti;
  11. temp1 = str[endi-1];
  12. temp = str[starti];
  13.  
  14. if(starti>=length-starti)
  15. {
  16. return;
  17. }
  18. str[starti] = temp1;
  19. str[endi-1] = temp;
  20. starti++;
  21. return (str_reverse(str, length-1));
  22. }
  23.  
  24. int main()
  25. {
  26. string test= "lighter";
  27. str_reverse(test, 7);
  28. cout << "output= " << test;
  29. return 0;
  30. }

A few problems. One, look at this call and this function:

    string test= "lighter";
    str_reverse(test, 7);

    void str_reverse(char str[], int length)

It gave me a compile error until I changed test to a character array to match the function.

Two:
void str_reverse(char str[], int length)

return (str_reverse(str, length-1));

Not an error, or at least my compiler let me get away with it, but you are returning something from a void function here. Just call it. As to the rest of your problem, look carefully at the two arguments in this line and make sure they are what you want.:

return (str_reverse(str, length-1));
Last edited by VernonDozier; Sep 5th, 2008 at 11:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Recursive function not fully working

 
0
  #3
Sep 6th, 2008
The first part was just a stub. I had the wrong string in my mind. I'm still not seeing whats wrong. Could you explain a little farther.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Recursive function not fully working

 
0
  #4
Sep 6th, 2008
Originally Posted by nizbit View Post
The first part was just a stub. I had the wrong string in my mind. I'm still not seeing whats wrong. Could you explain a little farther.
Post the exact code that compiles and gives you the problem, along with your results, whether it is working or not. To help you debug, place the following line:

  1. cout << str << endl;

before the recursive call so you can see each iteration. This may help you see what is occurring.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Recursive function not fully working

 
0
  #5
Sep 6th, 2008
A lot of the code in your function is superfluous. Try simplifying it to what you actually need. What is the point of starti? it's always read as 0. starti is a local non-static variable, so your starti++; at the end of the function does nothing useful.

Also, in the recursive call to reverse, length-1 should be length-2, as you've just swapped the first AND last characters (that's 2 characters). You'll also want to increment the pointer for each call so that you don't keep swapping the first character with every other character.

  1. void reverse(char *str, int len){
  2. if(len >= 2){
  3. swap(str[0], str[len-1]); // swap first & last chars
  4.  
  5. reverse(&str[1], len - 2); // swap the rest - (but not the chars we just swapped)
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Recursive function not fully working

 
0
  #6
Sep 6th, 2008
Thanks for the help everyone. My recursive function is working!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC