Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
reverse a string with 0 terminator in one pass
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Sana, post: 3810270"] Great clue right there. It implies some sort of recursion. So, what does reversing implies? Set the value of element in position [strlen(string) - i - 1] with the value of the element in the position i hmm, how do we get the length of the string without using strlen? we call recursively our helper function, each time with an incremented index into the string until we reach the end of the string. Once we are there, we return the length (the index of the element with the value '\0'). Upon returning we now know the length of the string. // what index do we start with? int processString(int index, char* string) { // you need to do store something in a variable here if (string[index] == '\0') return index; // the length of the string // not at the end of the string - call same function recursively with an incremented index int stringLength = processString(index + 1, string); // here you have the index and the length // see how you can use them to set the element [len - i - 1] // you will need the value stored in the variable at the begining of the function return stringLength; } HTH [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
reverse a string with 0 terminator in one pass
Top