How to take first letter from C string?

81,806

Solution 1

As strings are array of characters, you need to take the first element from the array:

char firstname_initial;
firstname_initial = firstname[0]

Also note that since lastname and firstname are buffers, you don't need to pass a pointer to them in scanf:

scanf( "%s", firstname );
scanf( "%s", lastname );

And one last thing - scanf is a dangerous function and you should not use it.

Solution 2

Suppose the user types:

Michael

in response to the first prompt. The %c format reads the M; the %s format reads ichael without bothering to get any new data.

Also, you should not be passing &firstname or &lastname; you should be passing just firstname and lastname to scanf(). The difference is in the type; with the ampersand, you're passing a char (*)[256] which is not the same as the char * that scanf() expects. You get away with it, but 'get away with it' is the operative term.

Use a %s format (or, better, %255s format) for the two scanf() calls. Then pass firstname[0] and lastname to printf(). You might want to think about using tolower() from <ctype.h> on the first letter, and maybe on the last name too.

This is a reasonable approximation to a good program:

#include <stdio.h>

int main(void)
{
    char firstname[256];
    char lastname[256];
    printf("What's your first name? ");
    if (scanf("%255s", firstname) != 1)
        return 1;
    printf("What's your last  name? ");
    if (scanf("%255s", lastname) != 1)
        return 1;
    printf("Your school.edu e-mail address is: %c%[email protected]\n",
           firstname[0], lastname);
    return 0;
}

It avoids quite a lot of problems, one way or another. It is not completely foolproof, but most people won't run into problems with it.

Share:
81,806

Related videos on Youtube

user2901857
Author by

user2901857

Updated on March 02, 2020

Comments

  • user2901857
    user2901857 about 4 years

    I want to take the first letter from my firstname string variable and add it to the second letter of the lastname variable.

    My program so far is:

    #include <stdio.h>
    
    main() {
    char firstname [256];
    char lastname [256];
    printf("What's your first name?: ");
    scanf("%c",&firstname);
    printf("What is your last name? ");
    scanf("%s",&lastname);
    printf("\nYour school.edu e-mail address is: %c%[email protected]",firstname,lastname);
    return 0;
    }
    

    However, I would like for my code to take the first initial (the first letter of the first name) and store it into the firstname variable.

    • Dayal rai
      Dayal rai over 10 years
      Are you facing problem in accessing a array element or overwriting in a existing array?
    • Abhineet
      Abhineet over 10 years
      Why are you having an array of 256 chars for "firstname" when you are only interested in and storing the first character?
    • Abhineet
      Abhineet over 10 years
      Kindly post the expected input and expected output.
  • user2901857
    user2901857 over 10 years
    Now I get the error: subscripted value is neither an array nor pointer
  • user2901857
    user2901857 over 10 years
    Got rid of the error. However now when I type in firstname it just goes [email protected] without even asking me to scan a second name and it doesnt take any intials
  • Jonathan Leffler
    Jonathan Leffler over 10 years
    You have various problems. You can't copy firstname[0] like that with strcpy(). You also can't do it with lastname[1], and you probably don't want to do it anyway (lastname or &lastname[0] instead). You allocated 50 bytes for the composite name, but 256 for the last name; this could lead to crashes.