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:
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:
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.:
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:
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.
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.