Thursday 20 September 2012

C program to reverse a string using recursion


#include<stdio.h>
#include<string.h>
 
void reverse(char*,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 beg, int end)
{
   char a, b, c;
 
   if ( beg >= end )
      return;   
 
   c = *(x+beg);
   *(x+beg) = *(x+end);
   *(x+end) = c;
 
   reverse(x, ++beg, --end);
}

4 comments:

  1. can you please help me in the concept of string reverse, each line of a file.using recursive function.
    for eg:INPUT is hai one
    hai two
    OUTPUT:
    hai two
    hai one

    ReplyDelete
    Replies
    1. TRY TO DO ,

      HAI ONE IN 1 MODULE AND HAI TWO IN SECOND MO-DUAL . NOW RIGHT TO LEFT READ THE STRING AND DISPLAY . OR YOU CAN DO SWAPING ALSO FOR DISPLAYING THIS . MANY METHODS ARE THERE ....

      Delete
    2. thank u frnd.....can u please tell the c coding for finding memory usage of a system??????

      Delete
  2. SURE ..... SEE OTHER SAME POST OF YOUR'S

    ReplyDelete