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.

#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;
}

Recommended Answers

All 8 Replies

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:

int start = 0;
int end = n - 1;

while ( start < end ) {
  std::swap ( a[start], a[end] );
  ++start;
  --end;
}

>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?

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:

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.

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

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.

#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;
}

>recursiveReverse(a[], ++start, --end);
You don't use [] when passing an array to a function.

Consider this solved :) Thanks Narue

#include <iostream>
#include <string>
using namespace std;


string reverse ( string s )
{
       if( s.size() == 0 )
           return s;
       if( s.size() == 1 )
           return s;
       else
           return reverse( s.substr(1,s.size()) ) + s.substr(0,1);
}

int main()
{
    string v = "Color";

    cout <<reverse( v )<<endl;

    system("pause");
    return 0;
}

This is the simplest solution... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.