Home Forums C Programming please help me in this… Reply To: please help me in this…

#3454
Humayan
Participant

this first code assumes one space between words:
//this code assumes only one space between words

#include
#include
#include
void main()
{
    int i,l;
    char name [ 40 ];
    //clrscr ( );
    printf ( "enter ur name:" );
    gets ( name );
    l = strlen ( name );
 printf ( "%c" , name [ 0 ] );
 printf ( " " );
    int countOfSpaces = 0;
    for( i = 0 ; i <= l ; i ++ )
    {
        if( name [ i ] == ' '  && countOfSpaces < 1 )
        {
            printf( "%c" , name [ i + 1 ] , " " );
   printf ( " " );
   countOfSpaces ++;
  }
        else if ( name [ i ] == ' ' && countOfSpaces == 1 )
   printf( "%s" , name + i + 1  );  
   
  
    }
    getch();
}

This second code assumes more than one space between words:


//******************************************************
//this second code assumes more than one space between words
 
#include < stdio.h >
#include < conio.h >
#include < string.h >
#include < ctype.h >
void main()
{
    int i , l , k;
    char name [ 40 ];
   // clrscr ( );
    printf ( "enter ur name:" );
    gets ( name );
    l = strlen ( name );
    printf ( "%c" , name [ 0 ] );
    printf ( " " );
 int countOfSpaces = 0;
    for( i = 0 ; i <= l ; i ++ )
    {
        if( isspace ( name [ i ] )  && countOfSpaces < 1 )
        {
            k = 0;
            while ( isspace ( name [ i ] ) && name [ i ] != 0 )
                i ++;
            printf ( "%c" , name [ i + 1 ] );
   printf ( " " );
   countOfSpaces ++;
  }
        else if ( name [ i ] == ' ' && countOfSpaces == 1 )
        {
           
            while ( isspace ( name [ i ] ) && name [ i ] != 0 )
                i ++;
            printf ( "%s" , name + i  );
        }
    }
    getch();
}