954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reverse a string using recursion

Hi,
Can somebody write a sample code to reverse a string using recursion.
Basically a function which takes string as input parameter, reverses the string using recusrion and returns back the reversed string to calling function.

Thank you
Naveen

varun51
Newbie Poster
2 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Post what you have put up tilll now and we will definately help you out.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Post what you have put up tilll now and we will definately help you out.



I have written this code:

#include <stdio.h>
void rev_str(char* s)
{
 
    if(*s != '\0')
         rev_str(s+1);
 
    
 printf("%c",*s);
}
 
int main()
{
   rev_str("born2c0de");
   return 0;
}


Here i am able to reverse the string and print it out...but i want rev_str function to return the reversed string or ideally reverse the passed string itself.

Can you help me out?

varun51
Newbie Poster
2 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Pass in another string which will be loaded with the reversed string as you go through your return side of the function

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
#include<conio.h>
char* reverse(char *s,int start,int last){
	char temp;
	if(start>=last){
	return s;
	}else{
	temp = s[start];
	s[start]=s[last];
	s[last]=temp;
	reverse(s,start+1,last-1);
	}
}
main(){
char *s="visha";
clrscr();
s = reverse(s,0,strlen(s)-1);
printf("\n ====> %s lenght : %d ",s,strlen(s));
}
gfhgh
Newbie Poster
1 post since Feb 2010
Reputation Points: 8
Solved Threads: 0
 

Leaving aside the 3+ year grave digging...

> char *s="visha";
This MAY be in read-only memory. It certainly is for most modern compilers on most modern operating systems. But since you seem to be using crusty old Turbo C (why I don't know), it will "work for you".

> s[start]=s[last];
Being read-only memory, any attempt to MODIFY said string constant will immediately result in your program being killed off by the OS.

> reverse(s,start+1,last-1);
Your if part returns a result, but this doesn't.
The result will be that the function will return random garbage to the caller (for any non-trivial string). Again, you may be getting lucky. Others who try this code will fare less well.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Here's a better version of gfhgh's code:

#include <stdio.h>
#include <string.h>

void reverse(char **s, int start, int last)
{
	char tmp;
	if (start >= last)
		return;
	char *s2 = *s;
	tmp = s2[start];
	s2[start] = s2[last];
	s2[last] = tmp;
	reverse(s, start + 1, last - 1);
}

int main()
{
	char *s = strdup("Hello World");
	printf("%s\n", s);
	reverse(&s, 0, strlen(s) - 1);
	printf("%s\n", s);
}
stringreversal
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Here is another version:

You could also pass the array to the function instead of making it global.

char arr[100] = "madam";

int reverse(int start, int last)
{
	
	if (arr[start] != arr[last])
		return 0;	

	if (start >= last)
		return 1;
	
	else 
		return reverse(start + 1, last - 1);
	
}
myk45
Posting Whiz
319 posts since Sep 2010
Reputation Points: 57
Solved Threads: 40
 

#include

void rev(char *l, char *r);


int main(int argc, char *argv[])
{
char buf[] = "the world will go on forever";
char *end, *x, *y;

// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);


// Now swap each word within sentence...
x = buf-1;
y = buf;

while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}

// Now print the final string....
printf("%s\n",buf);

return(0);
}

sanjoy saha
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

void reverse (int index char *str ) ;

int main (void)

{

char name[100];

printf ( Enter a mutli-word string ) ;

gets(name) ;

reverse (strlen(name) name ) ;

}

void reverse (int index char *str )

{

if (--index < 0 )

{

return ;

}

else

{

putchar ( *(str + index) ) ;

reverse (index str) ;

}

}

searchsource
Newbie Poster
14 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You