Google+ Program For Reverse a String in C[How to] - CodieeHome
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
__

                                                          Reversing   String


  Goal:- Reverse a string in c.


Function Used:- 

                            char * strrev(char s[]);

                This function replace string s with it reverse and  returns the address of s.


Explanation:-

               There are three different ways via we can reverse a string:-

              1.Using string function strrev().


             2.Without using string function.




1.Using string function:-

               In this we simply pass the given string to strrev() and return value of it stores in a pointer.Use this pointer to Display reversed string.


2.Without Using string function:-

               We have to methods to reverse a string except string function method:-

               1.By simply copying the each character in  another string in reverse order.

               2.Using pointers.

Program:-

Given Program reverse a string using 3 methods mentioned above.



#include<stdio.h>


#include<string.h>


#include<conio.h>


int main()

       {

           char s[10],r[10];


           char *re,*er=r;


           int j,i;


           printf("Enter a String you want to Reverse:-");


           scanf("%s",s);

           //Using strrev-

           re=strrev(s);// It replaces the string s with its reverse and return its address.

           printf("\nReversed String(Using strrev)-%s",re);

           //Without strrev--

           i=0;

           while(s[i]!='\0'){

           i++;

           }

           j=0;

           while(i>0)
          {

              r[j++]=s[--i];

            }

           r[j]='\0';

  printf("\n\nReversed string(Without using strrev)%s",r);

            //with the help of Pointers
            while(*re)
             {

               re++;

               i++;

             }

             while(i>=0)
              {

                 re--;

                 *er=*re;

                  er++;

                  i--;

             }  

              er='\0';

              printf("\n\n Reversed string (Using Pointers)-%s",r);
              
             getch();
    }    
                

 Output:-

        If you want to share more information about topic discussed above or find anything incorrect Please do comments.    

           






          


0 comments:

Post a Comment

 
Top