Thursday, August 8, 2013

reversing a given string using recursion C program!!

Below is the simple C program for reverse a given string using recursion.  Check here for non-recursive ways to reverse a string.


void reverse(char *str2,char *str1 )
     str2[strlen(str1) - 1] = str1[0];
     reverse(str2, str1+1);

int main()
  char str1[20],str2[20];

  printf("Enter a string for str1 :");
  printf("\n Source string:%s",str1);

  memset (str2, 0, 20);

  reverse (str2, str1);
  printf(" \n Destination string:%s \n",str2);

  return 0;

Sample Output: 
warning: this program uses gets(), which is unsafe.
Enter a string for str1 :reverse

Source string:reverse
Destination string:esrever

Let me know or comment below for any info or some bug if you find in the above code.

No comments:

Popular Posts