C语言 如何从C字符串中取第一个字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take first letter from C string?
提问by user2901857
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.
但是,我希望我的代码采用第一个首字母(名字的第一个字母)并将其存储到 firstname 变量中。
回答by MByD
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 lastnameand firstnameare buffers, you don't need to pass a pointer to them in scanf:
另请注意,由于lastname和firstname是缓冲区,因此您无需在 中传递指向它们的指针scanf:
scanf( "%s", firstname );
scanf( "%s", lastname );
And one last thing - scanfis a dangerous function and you should not use it.
最后一件事 -scanf是一个危险的功能,你不应该使用它。
回答by Jonathan Leffler
Suppose the user types:
假设用户类型:
Michael
in response to the first prompt. The %cformat reads the M; the %sformat reads ichaelwithout bothering to get any new data.
响应第一个提示。的%c格式读取M; 该%s格式的读取ichael,而不会打扰得到任何新的数据。
Also, you should not be passing &firstnameor &lastname; you should be passing just firstnameand lastnameto 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.
此外,你不应该通过&firstname或&lastname; 你应该刚好路过firstname并lastname给scanf()。区别在于类型;与符号,你传递char (*)[256]这是不一样的char *是scanf()期待。你可以侥幸逃脱,但“侥幸逃脱”是有效术语。
Use a %sformat (or, better, %255sformat) for the two scanf()calls. Then pass firstname[0]and lastnameto printf(). You might want to think about using tolower()from <ctype.h>on the first letter, and maybe on the last name too.
为两个调用使用一种%s格式(或者更好的%255s格式)scanf()。然后通过firstname[0]和lastname到printf()。您可能想考虑在第一个字母上使用tolower()from <ctype.h>,也可能在姓氏上使用。
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.
它以一种或另一种方式避免了很多问题。它并非完全万无一失,但大多数人不会遇到问题。
回答by Imtiaz Emu
#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;
}
回答by Imtiaz Emu
I think you want the variable firstnameto store only the initial. So that firstnameact like string.
我认为您希望变量firstname仅存储首字母。所以名字就像字符串一样。
firstname[1] = 'fname = Batman
lname = Joker
'; //mark the end of string on second character
printf("\nYour school.edu e-mail address is: %s%[email protected]",firstname,lastname);
回答by Abhineet
Assuming expected input:
假设预期输入:
Your school.edu e-mail address is: [email protected]
Expected Output:
预期输出:
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;
}
Try this:
尝试这个:
char extractedchar = '0';
extractedchar=myoldstring[0];
回答by intika
Copy first char from c string
从 c 字符串复制第一个字符
##代码##Note : the char '0' is there just to test later in the application
注意:字符“0”只是为了稍后在应用程序中进行测试

