Monday, March 23, 2015

C program to reverse string using recursion

#include<stdio.h>
void reverse(char*,int,int);

int main()
{
char a[100];
gets(a);
reverse(a,0,strlen(a)-1);
printf("%s\n",a);
return 0;
}

void reverse(char *x,int i,int j)
{
char c;
if(i>=j)
return;
c=*(x+i);
*(x+i)=*(x+j);
*(x+j)=c;

reverse(x,++i,--j);
}

No comments: