Thread: Getting first letter of name

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Getting first letter of name

    Ok i got this code so far but iam sorta stuck right now.

    Code:
    #include <stdio.h>
    void strip_newline( char *str, int size )
    {
         int i;
         for (  i = 0; 1 < size; i++ )
         {
             if ( str[i] == '\n' )
             {
                  str[i] = '\0';
                  }
                  return;
             }
         }
         
    int main()
    {
        char name[50];
        char letter[0];/*='k'*/
        
        printf("Please enter your first name:");
        scanf( "%s", letter );
        
    }
    Iam stuck on how to make it get the first letter of the name

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
         
    int main(void)
    {
        char name[50];
        printf("Please enter your first name: ");
        fflush(stdout);
        fgets(name, sizeof name, stdin);
        printf("The first letter is %c\n", name[0]);
        return 0;
    }
    
    /* my output
    Please enter your first name: Dave
    The first letter is D
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for ( i = 0; 1 < size; i++ )
    LOL - I can't believe you're still working with this broken code - you've posted the same code over and over, and never fixed it.

    > return;
    After peering through your broken indentation, it seems this function examines only one character and then exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Code:
    > for ( i = 0; 1 < size; i++ )
    Still works, it compiles and returns the repsonse i wanted it to return to me.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    *ahem* i is not the same as 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    I fixed it But next time ill look carefully at what i type

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating DNA sequence
    By Teiji in forum C Programming
    Replies: 54
    Last Post: 04-08-2009, 09:08 PM
  2. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  3. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  4. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  5. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM