C - 使用逗号与strtok分隔名称和姓氏

时间:2014-04-06 16:21:20

标签: strtok

我有strtok的问题。我正在尝试使用逗号分隔名字和姓氏。当我尝试使用scanf并输入“Vincent,Van Gogh”时,它返回Vincent作为名字,但姓氏只返回Van。

但是,当我使用预设字符串时,它会将姓氏显示为梵高。

printf("Enter name and surname: ");
char name[100];// = "Vincent,Van Gogh";
char firstname [100];
char lastname [100];
char* token;

scanf("%s", &name);
token = strtok(name, ",");
strcpy(firstname, token);
token = strtok(NULL, ",");
strcpy(lastname, token);

puts(firstname);
puts(lastname);

为了让用户输入有效,我是否缺少一些东西?

0 个答案:

没有答案