Home › Forums › C Programming › Reverse a string without using library function
- This topic has 2 replies, 2 voices, and was last updated 16 years, 11 months ago by Humayan.
Viewing 2 reply threads
- AuthorPosts
- December 18, 2007 at 4:40 am #2048sreedhanyaParticipant
how to write c program to reverse a string without using library function & without loop?
- December 18, 2007 at 2:48 pm #3297HumayanParticipant
Try
C123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354/***************************************************************** 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 : reverseStringParameters :string a(n) char * ( char * )Returns: user defined type , voidComments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/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;} - December 19, 2007 at 1:34 pm #3298HumayanParticipant
Here’s one without a loop:
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/***************************************************************** 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 : reverseStringParameters :string a(n) char * ( char * ) ,front a(n) int ( int ) ,rear a(n) int ( int )Returns: Void typeComments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/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 : reverseStringDriverParameters :string a(n) char *Returns: Void typeComments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void reverseStringDriver ( char * string , int length ){int front = 0 , rear = length - 1;reverseString ( string , front , rear );return;}
- AuthorPosts
Viewing 2 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.