Recursively Reversing a String

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

Recursively Reversing a String

 
0
  #1
Oct 6th, 2007
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.
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void recursiveReverse( char a[], int start, int end )
  6. {
  7. int i, j;
  8.  
  9. for(i=start; i<end; i++)
  10. {
  11.  
  12. for(j=end;j>start;j--)
  13. {
  14. char temp = a[i];
  15. a[i]=a[j];
  16. a[j]=temp;
  17. }
  18.  
  19. }
  20.  
  21. }
  22.  
  23.  
  24.  
  25. void stringReverse( char str[] )
  26. {
  27. int length = strlen( str );
  28. recursiveReverse( str, 0, length - 1 );
  29. }
  30.  
  31. int main()
  32. {
  33.  
  34. char a[20];
  35.  
  36. for (int i = 0; i < 20; i++ )
  37. a[i] = 'A' + i;
  38. a[19] = '\0';
  39. cout << "string before reversing: " << a << endl;
  40. stringReverse( a );
  41. cout << "string after reversing: " << a << endl;
  42. return 0;
  43. }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Recursively Reversing a String

 
0
  #2
Oct 6th, 2007
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.
Yep, that's the way to do it. Usually you'd use a single loop though:
  1. int start = 0;
  2. int end = n - 1;
  3.  
  4. while ( start < end ) {
  5. std::swap ( a[start], a[end] );
  6. ++start;
  7. --end;
  8. }
>If you guys have any suggestions or advice I would really appreciate it.
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Recursively Reversing a String

 
0
  #3
Oct 7th, 2007
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:
  1. void ReverseString(char* s)
  2. {
  3. if( *s != '\0')
  4. ReverseString(s+1);
  5.  
  6. cout<<*s;
  7. }

Maybe this will help. Not my code, actually did it before in C, sorta.. but hell it's all over the net. ><..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Recursively Reversing a String

 
0
  #4
Oct 7th, 2007
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Recursively Reversing a String

 
0
  #5
Oct 7th, 2007
@Narue
Cool thankies for the info. They didn't thought us this recursion back in class, so I didn't had much idea. thank you, I just have try out some programs to get the indirect recursion.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

Re: Recursively Reversing a String

 
0
  #6
Oct 13th, 2007
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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void recursiveReverse( char a[], int start, int end )
  6. {
  7. while(start<end)
  8. {
  9. std::swap ( a[start], a[end] );
  10. recursiveReverse(a[], ++start, --end);
  11. }
  12. }
  13.  
  14.  
  15.  
  16. void stringReverse( char str[] )
  17. {
  18. int length = strlen( str );
  19. recursiveReverse( str, 0, length - 1 );
  20. }
  21.  
  22. int main()
  23. {
  24.  
  25. char a[20];
  26.  
  27. for (int i = 0; i < 20; i++ )
  28. a[i] = 'A' + i;
  29. a[19] = '\0';
  30. cout << "string before reversing: " << a << endl;
  31. stringReverse( a );
  32. cout << "string after reversing: " << a << endl;
  33. return 0;
  34. }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Recursively Reversing a String

 
0
  #7
Oct 13th, 2007
>recursiveReverse(a[], ++start, --end);
You don't use [] when passing an array to a function.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

Re: Recursively Reversing a String

 
0
  #8
Oct 13th, 2007
Consider this solved Thanks Narue
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC