Home › Forums › C Programming › function
- This topic has 2 replies, 2 voices, and was last updated 13 years, 1 month ago by
Humayan.
Viewing 2 reply threads
- AuthorPosts
- December 18, 2007 at 4:40 am #2048
sreedhanya
Participanthow to write c program to reverse a string without using library function & without loop?
- December 18, 2007 at 2:48 pm #3297
Humayan
Participanttry:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455<br />/****************************************************************<br />* File Name : c:programshelpshell.cpp<br />* Date : December,18,2007<br />* Comments : new project<br />* Compiler/Assembler :<br />* Program Shell Generated At: 3:35:40 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br />#include <iostream><br />#include <string.h ><br />//#include <conio.h ><br />//#include <math.h ><br />//#include <iomanip><br />//#include <ctype.h ><br />using namespace std;<br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br />void reverseString ( char * string , int length );<br />//##################################################################################<br /><br />//main function ******************************<br />int main ( )<br />{<br />char string [ 13 ];<br />strcpy ( string , "hello theres" );<br />reverseString ( string , 12 );<br />cout << "reversed string: " << string << endl;<br /><br />return 0 ;<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br />Name : reverseString<br />Parameters :<br />string a(n) char * ( char * )<br /><br />Returns: user defined type , void<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void reverseString ( char * string , int length )<br />{<br />int i = length - 1;<br />int j = 0;<br />char temp;<br />while ( j <= i )<br />{<br />temp = string [ j ];<br />string [ j ] = string [ i ];<br />string [ i ] = temp;<br />j ++;<br />i --;<br />}<br />return;<br />}</iomanip></iostream> - December 19, 2007 at 1:34 pm #3298
Humayan
Participantheres one without a loop:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970/****************************************************************<br />* File Name : c:programshelpshell.cpp<br />* Date : December,19,2007<br />* Comments : new project<br />* Compiler/Assembler :<br />* Program Shell Generated At: 2:21:22 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br />using namespace std;<br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br />void reverseString ( char * string , int & front , int & rear );<br />void reverseStringDriver ( char * string , int length );<br />//##################################################################################<br /><br />//main function ******************************<br />int main ( )<br />{<br />char string [ 10 ] ;<br />strcpy ( string , "hellos" );<br />reverseStringDriver ( string , 6 );<br />cout << "reversed string : " << string << endl ;<br />return 0 ;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br />Name : reverseString<br />Parameters :<br />string a(n) char * ( char * ) ,<br />front a(n) int ( int ) ,<br />rear a(n) int ( int )<br />Returns: Void type<br />Comments:<br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void reverseString ( char * string , int & front , int & rear )<br />{<br /><br />if ( front > rear )<br />return;<br />char temp;<br /><br />temp = string [ front ];<br />string [ front ] = string [ rear ];<br />string [ rear ] = temp;<br /><br />front ++;<br />rear --;<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br />Name : reverseStringDriver<br />Parameters :<br />string a(n) char *<br /><br />Returns: Void type<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void reverseStringDriver ( char * string , int length )<br />{<br />int front = 0 , rear = length - 1;<br />reverseString ( string , front , rear );<br />return;<br />}<br />
- AuthorPosts
Viewing 2 reply threads
- You must be logged in to reply to this topic.