| | |
Recursive function not fully working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 78
Reputation:
Solved Threads: 0
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)
#include <iostream> #include <string> using namespace std; void str_reverse(char str[], int length) { char temp, temp1; int starti=0; int endi = length-starti; temp1 = str[endi-1]; temp = str[starti]; if(starti>=length-starti) { return; } str[starti] = temp1; str[endi-1] = temp; starti++; return (str_reverse(str, length-1)); } int main() { string test= "lighter"; str_reverse(test, 7); cout << "output= " << test; return 0; }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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)
#include <iostream> #include <string> using namespace std; void str_reverse(char str[], int length) { char temp, temp1; int starti=0; int endi = length-starti; temp1 = str[endi-1]; temp = str[starti]; if(starti>=length-starti) { return; } str[starti] = temp1; str[endi-1] = temp; starti++; return (str_reverse(str, length-1)); } int main() { string test= "lighter"; str_reverse(test, 7); cout << "output= " << test; return 0; }
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.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
cout << str << endl;
before the recursive call so you can see each iteration. This may help you see what is occurring.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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
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.
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)
void reverse(char *str, int len){ if(len >= 2){ swap(str[0], str[len-1]); // swap first & last chars reverse(&str[1], len - 2); // swap the rest - (but not the chars we just swapped) } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: .exe and then disappears
- Next Thread: need help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






