user2901857
user2901857

Reputation: 51

How to take first letter from C string?

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.

Upvotes: 5

Views: 99428

Answers (6)

Abhineet
Abhineet

Reputation: 5409

Assuming expected input:

fname = Batman
lname = Joker

Expected Output:

Your school.edu e-mail address is: [email protected]

Try this:

void main( void )
{
char fname = 0;
char lname[256] = {0};

printf("Enter firstname\n");
scanf("%c", &fname);

printf("Enter lastname\n");
scanf("%s", lname);

lname[1] = fname;

printf("Your school.edu e-mail address is: %c%[email protected]\n", fname, lname);

return;
}

Upvotes: 0

intika
intika

Reputation: 9792

Copy first char from c string

char extractedchar = '0';
extractedchar=myoldstring[0];

Note : the char '0' is there just to test later in the application

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754900

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.

Upvotes: 3

user2901535
user2901535

Reputation:

I think you want the variable firstname to store only the initial. So that firstname act like string.

firstname[1] = '\0'; //mark the end of string on second character
printf("\nYour school.edu e-mail address is: %s%[email protected]",firstname,lastname);

Upvotes: 1

Imtiaz Emu
Imtiaz Emu

Reputation: 489

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

Upvotes: 0

MByD
MByD

Reputation: 137412

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.

Upvotes: 4

Related Questions