Home › Forums › C Programming › please help me in this… › Reply To: please help me in this…
September 10, 2008 at 11:19 am
#3454
Humayan
Participant
this first code assumes one space between words:
//this code assumes only one space between words
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 | #include<stdio.h><br /> #include<conio.h><br /> #include<string.h><br /> void main()<br /> {<br /> int i,l;<br /> char name [ 40 ];<br /> //clrscr ( );<br /> printf ( "enter ur name:" );<br /> gets ( name );<br /> l = strlen ( name );<br /> printf ( "%c" , name [ 0 ] );<br /> printf ( " " );<br /> int countOfSpaces = 0;<br /> for( i = 0 ; i <= l ; i ++ )<br /> {<br /> if( name [ i ] == ' ' && countOfSpaces < 1 )<br /> {<br /> printf( "%c" , name [ i + 1 ] , " " );<br /> printf ( " " );<br /> countOfSpaces ++;<br /> }<br /> else if ( name [ i ] == ' ' && countOfSpaces == 1 )<br /> printf( "%s" , name + i + 1 ); <br /> <br /> <br /> }<br /> getch();<br /> } |
This second code assumes more than one space between words:
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 | <br /> //******************************************************<br /> //this second code assumes more than one space between words<br /> <br /> #include < stdio.h ><br /> #include < conio.h ><br /> #include < string.h ><br /> #include < ctype.h ><br /> void main()<br /> {<br /> int i , l , k;<br /> char name [ 40 ];<br /> // clrscr ( );<br /> printf ( "enter ur name:" );<br /> gets ( name );<br /> l = strlen ( name );<br /> printf ( "%c" , name [ 0 ] );<br /> printf ( " " );<br /> int countOfSpaces = 0;<br /> for( i = 0 ; i <= l ; i ++ )<br /> {<br /> if( isspace ( name [ i ] ) && countOfSpaces < 1 )<br /> {<br /> k = 0;<br /> while ( isspace ( name [ i ] ) && name [ i ] != 0 )<br /> i ++;<br /> printf ( "%c" , name [ i + 1 ] );<br /> printf ( " " );<br /> countOfSpaces ++;<br /> }<br /> else if ( name [ i ] == ' ' && countOfSpaces == 1 )<br /> {<br /> <br /> while ( isspace ( name [ i ] ) && name [ i ] != 0 )<br /> i ++;<br /> printf ( "%s" , name + i );<br /> }<br /> }<br /> getch();<br /> }<br /> |