Home › Forums › C Programming › Reverse a string without using library function › Reply To: Reverse a string without using library function
December 19, 2007 at 1:34 pm
#3298
Humayan
Participant
Here’s one without a loop:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /**************************************************************** * 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; } |