| | |
Recursively Reversing a String
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello again
,
I am writing a code that will recursively write a string in reverse. The problem is in my recursiveReverse function. I feel like the idea is right, but the implementation is incorrect. My thought process was that I could start the loops at the beginning and the end of the string and then swap the two characters. Then, I could increase the iteration of the beginning by one and decrease the iteration of the end by one, and the repeat the swap. If you guys have any suggestions or advice I would really appreciate it. Thanks again ahead of time.
,I am writing a code that will recursively write a string in reverse. The problem is in my recursiveReverse function. I feel like the idea is right, but the implementation is incorrect. My thought process was that I could start the loops at the beginning and the end of the string and then swap the two characters. Then, I could increase the iteration of the beginning by one and decrease the iteration of the end by one, and the repeat the swap. If you guys have any suggestions or advice I would really appreciate it. Thanks again ahead of time.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void recursiveReverse( char a[], int start, int end ) { int i, j; for(i=start; i<end; i++) { for(j=end;j>start;j--) { char temp = a[i]; a[i]=a[j]; a[j]=temp; } } } void stringReverse( char str[] ) { int length = strlen( str ); recursiveReverse( str, 0, length - 1 ); } int main() { char a[20]; for (int i = 0; i < 20; i++ ) a[i] = 'A' + i; a[19] = '\0'; cout << "string before reversing: " << a << endl; stringReverse( a ); cout << "string after reversing: " << a << endl; return 0; }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
-George Carrette
•
•
•
•
My thought process was that I could start the loops at the beginning and the end of the string and then swap the two characters. Then, I could increase the iteration of the beginning by one and decrease the iteration of the end by one, and the repeat the swap.
C++ Syntax (Toggle Plain Text)
int start = 0; int end = n - 1; while ( start < end ) { std::swap ( a[start], a[end] ); ++start; --end; }
I only have one complaint. The algorithm you've described and implemented isn't recursive at all. Are you sure you're doing what you need to be doing?
New members chased away this month: 3
Well if it's a recursive function, then why are you using two functions? it's just calling function. I don't know much in detail about this recursion, but from all th examples in my books, I know that you need to all the same function (one you declare) again and again to get the result.
What you are throwing is literally throwing it off.
try this:
Maybe this will help. Not my code, actually did it before in C, sorta.. but hell it's all over the net. ><..
What you are throwing is literally throwing it off.
try this:
C++ Syntax (Toggle Plain Text)
void ReverseString(char* s) { if( *s != '\0') ReverseString(s+1); cout<<*s; }
Maybe this will help. Not my code, actually did it before in C, sorta.. but hell it's all over the net. ><..
>I know that you need to all the same function (one you declare)
>again and again to get the result.
Yes, either directly or indirectly. Otherwise it's not recursion. By directly, the a function calls itself without any intervening calls. Indirectly is when a function calls other functions that eventually call it again.
>again and again to get the result.
Yes, either directly or indirectly. Otherwise it's not recursion. By directly, the a function calls itself without any intervening calls. Indirectly is when a function calls other functions that eventually call it again.
New members chased away this month: 3
Thanks for the replies guys.
Didn't realize that what I was doing wasn't conversion heh. So, after looking at it, why wouldn't I be able to do something like this in the recursiveReverse function. I am getting a syntax error ']' when I try to recursively pass the the recursiveReverse function. Thanks again guys for the responses.
Didn't realize that what I was doing wasn't conversion heh. So, after looking at it, why wouldn't I be able to do something like this in the recursiveReverse function. I am getting a syntax error ']' when I try to recursively pass the the recursiveReverse function. Thanks again guys for the responses.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void recursiveReverse( char a[], int start, int end ) { while(start<end) { std::swap ( a[start], a[end] ); recursiveReverse(a[], ++start, --end); } } void stringReverse( char str[] ) { int length = strlen( str ); recursiveReverse( str, 0, length - 1 ); } int main() { char a[20]; for (int i = 0; i < 20; i++ ) a[i] = 'A' + i; a[19] = '\0'; cout << "string before reversing: " << a << endl; stringReverse( a ); cout << "string after reversing: " << a << endl; return 0; }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
-George Carrette
![]() |
Similar Threads
- Reverse a String (VB.NET)
- Reversing a string (C++)
- I Need Assembly help w/reversing strings (Assembly)
- Reversing a string? (C++)
Other Threads in the C++ Forum
- Previous Thread: When passing 2D array, only last line displays
- Next Thread: Help with counter-controlled loop to find average
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






