943,700 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 911
  • C++ RSS
Sep 5th, 2008
0

Recursive function not fully working

Expand 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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nizbit is offline Offline
88 posts
since Sep 2008
Sep 5th, 2008
0

Re: Recursive function not fully working

Click to Expand / Collapse  Quote originally posted by nizbit ...
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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 6th, 2008
0

Re: Recursive function not fully working

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nizbit is offline Offline
88 posts
since Sep 2008
Sep 6th, 2008
0

Re: Recursive function not fully working

Click to Expand / Collapse  Quote originally posted by nizbit ...
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:

C++ Syntax (Toggle Plain Text)
  1. cout << str << endl;

before the recursive call so you can see each iteration. This may help you see what is occurring.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 6th, 2008
0

Re: Recursive function not fully working

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.

CPP Syntax (Toggle Plain Text)
  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. }
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 6th, 2008
0

Re: Recursive function not fully working

Thanks for the help everyone. My recursive function is working!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nizbit is offline Offline
88 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: .exe and then disappears
Next Thread in C++ Forum Timeline: need help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC