Home › Forums › C Programming › Reverse a string without using library function › Reply To: Reverse a string without using library function
December 18, 2007 at 2:48 pm
#3297
Humayan
Participant
Try
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 | /**************************************************************** * File Name : c:programshelpshell.cpp * Date : December,18,2007 * Comments : new project * Compiler/Assembler : * Program Shell Generated At: 3:35:40 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 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; } |