Home Forums C Programming function

Viewing 2 reply threads
  • Author
    Posts
    • #2048
      sreedhanya
      Participant

      how to write c program to reverse a string without using library function & without loop?

    • #3297
      Humayan
      Participant

      try:


      /****************************************************************
      * File Name : c:programshelpshell.cpp
      * Date : December,18,2007
      * Comments : new project
      * Compiler/Assembler :
      * Program Shell Generated At: 3:35:40 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
      #include
      #include
      //#include
      //#include
      //#include
      //#include
      using namespace std;

      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
      void reverseString ( char * string , int length );
      //##################################################################################

      //main function ******************************
      int main ( )
      {
      char string [ 13 ];
      strcpy ( string , "hello theres" );
      reverseString ( string , 12 );
      cout << "reversed string: " << string << endl;

      return 0 ;
      }

      /******************************* FUNCTION DEFINITION ******************************
      Name : reverseString
      Parameters :
      string a(n) char * ( char * )

      Returns: user defined type , void
      Comments:

      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void reverseString ( char * string , int length )
      {
      int i = length - 1;
      int j = 0;
      char temp;
      while ( j <= i )
      {
      temp = string [ j ];
      string [ j ] = string [ i ];
      string [ i ] = temp;
      j ++;
      i --;
      }
      return;
      }
    • #3298
      Humayan
      Participant

      heres one without a loop:

      /****************************************************************
      * File Name : c:programshelpshell.cpp
      * Date : December,19,2007
      * Comments : new project
      * Compiler/Assembler :
      * Program Shell Generated At: 2:21:22 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

      #include < iostream >
      //#include < string.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >
      using namespace std;

      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
      void reverseString ( char * string , int & front , int & rear );
      void reverseStringDriver ( char * string , int length );
      //##################################################################################

      //main function ******************************
      int main ( )
      {
      char string [ 10 ] ;
      strcpy ( string , "hellos" );
      reverseStringDriver ( string , 6 );
      cout << "reversed string : " << string << endl ;
      return 0 ;
      }
      /******************************* FUNCTION DEFINITION ******************************
      Name : reverseString
      Parameters :
      string a(n) char * ( char * ) ,
      front a(n) int ( int ) ,
      rear a(n) int ( int )
      Returns: Void type
      Comments:
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void reverseString ( char * string , int & front , int & rear )
      {

      if ( front > rear )
      return;
      char temp;

      temp = string [ front ];
      string [ front ] = string [ rear ];
      string [ rear ] = temp;

      front ++;
      rear --;
      return;
      }
      /******************************* FUNCTION DEFINITION ******************************
      Name : reverseStringDriver
      Parameters :
      string a(n) char *

      Returns: Void type
      Comments:

      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void reverseStringDriver ( char * string , int length )
      {
      int front = 0 , rear = length - 1;
      reverseString ( string , front , rear );
      return;
      }
Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.