Google Search

Sunday, February 14, 2016

C program to reverse a string using recursion

‪#‎include‬ <stdio.h>
#include <string.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 begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}

1 comment:

  1. We can reverse a string using recursion by breaking a problem into sub-problem using divide and conquer approach. Here is a simple c program to reverse a string

    ReplyDelete