在这里,我试图将名字和姓氏输入为“名字姓氏”。我想得到“姓氏,FN”的输出。其中 FN 是名字的第一个字母。
示例:输入 = "James Garcia" ; 输出 =“加西亚,J。”
请在我的代码中找到错误。
#include <stdio.h>
int main () {
char fn[20],ln[20];
printf("Enter a first name and a last name:");
scanf("%c %s",&fn,&ln);
printf("%s, %c.",ln,fn);
return(0);
}
你要这个:
printf("Enter a first name and a last name:");
scanf("%s %s", fn, ln); // you are reading two strings,
// not one char and one string
printf("%s, %c.", ln, fn[0]); // you print one string and one char
// not two chars
免责声明:这是过度简化的代码,为简洁起见,不进行任何错误检查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句