我是 C 的新学习者,我在组合由 strtok(( 分隔的字符串时遇到了麻烦。 使用克里昂 (C99( 让我们看看我的代码。
char recipient[30];
char final_destination[50];
char status[10];
printf("Please enter 1> Recipient-, 2> Final Destination- and
3>Delivery status :n"); Entered(L-Rat Kitchen-House Not-Arrived)
scanf("%29s%49s%9s", recipient, final_destination, status);
///只先接收者/在字符串中存储"收件人final_destination,状态"的目的是让用户在使用scanf时在同一行中输入数据。之后,我使用 strtok 分隔"-"之前/之后的单词。通过在scanf中使用空格键分隔不同的字符串,以防止数据输入,例如(L-ratKitchenHouseNot-arrive(。
const char s[] = "-";
char *token;
token = strtok(recipient, s);
while( token != NULL )
???????????????????????
token = strtok(NULL, s);
在?????????????? ,我卡住的行,我不知道如何或有没有办法将这些处理后的数据放在一个数组中并组合在一起。
期待:
输入:(L-R-Rat 房子-厨房未到达(
输出:(L 鼠屋厨房未到达(
如果有解决方案!!将非常有帮助
如果你想在数组中存储令牌,试试这个。
while (token != NULL)
{
printf("%sn", token);
token = strtok(NULL, "-");
}