Home Forums C Programming please help me in this…

Viewing 3 reply threads
  • Author
    Posts
    • #2135
      bajean
      Participant

      friends i want to make a simple program in c in which if i give input as brian charles lara then it should print b c lara

      i am able to print b c l
      reply asap!!!

    • #3452
      Humayan
      Participant

      Is this what you need? :
       

      /****************************************************************
      * File Name : c:programshelpnames.cpp
      * Date : September,9,2008
      * Comments : new project
      * Compiler/Assembler :
      * Program Shell Generated At: 3:55:05 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

      #include < stdio.h >
      #include < string.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >
      #define MAX_NAME_LENGTH 30
      //using namespace std;
       //main function ******************************
      int main ( )
      {
          char firstName  [ MAX_NAME_LENGTH ];
          char middleName [ MAX_NAME_LENGTH ];
          char lastName   [ MAX_NAME_LENGTH ];
         
       printf ( "Enter a full name " );
          scanf ( "%s" , firstName );
          scanf ( "%s" , middleName);
          scanf ( "%s" , lastName );
          printf ( "output: " );
          printf ( "%c" , firstName [ 0 ] );
          printf ( " " );
          printf ( "%c" , middleName [ 0 ] );
          printf ( " " );
          printf ( "%s" , lastName );
          printf ( "n" );
          return 0 ;
      }
    • #3453
      bajean
      Participant

      sir thanx for ur post,
      yaa…its good…..but sir is it necessary to input three strings…..???
      below written  program will output initials of the name… 


      #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]);
      for(i=0;i<=l;i++)
      {
      if(name==' ')
      {
      printf("%c",name[i+1]);
      }
      }
      getch();
      }

      can u modify this one??….to get the desired output….
       
       

    • #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();
      }
Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.