C 将字符串解析为参数,然后连接其中一些参数

C 将字符串解析为参数,然后连接其中一些参数,c,C,我的任务是制作家庭二叉树。在这项任务中,我需要实现一些命令(例如add、draw、list…),首先我需要标记标准输入。例如“add Walburga[f]mother Sirius Black[m]”,以查找输入的命令、姓名、姓氏、关系和性别。 我已经用strtok函数实现了这一点,现在我有了数组,它包含标准输入中的每个分隔字符串作为参数。但现在我需要将名称和姓氏(如果存在)连接为一个参数。我认为sprintf就是您想要的 char fullname[strlen(commands[4]) +

我的任务是制作家庭二叉树。在这项任务中,我需要实现一些命令(例如add、draw、list…),首先我需要标记标准输入。例如“add Walburga[f]mother Sirius Black[m]”,以查找输入的命令、姓名、姓氏、关系和性别。
我已经用strtok函数实现了这一点,现在我有了数组,它包含标准输入中的每个分隔字符串作为参数。但现在我需要将名称和姓氏(如果存在)连接为一个参数。

我认为
sprintf
就是您想要的

char fullname[strlen(commands[4]) + strlen(commands[5]) + 2]; // plus 2 for ' '  and '\0' 
sprintf(fullname, "%s %s", commands[4], commands[5]);
/* fullname = "Sirius Black" */

如果输入始终具有以下格式:

command fullname1 [gender] relation fullname2 [gender]
我建议使用
strchr()
查找第一个空格并将其称为
command\u end
。然后找到第一个
[
并调用[-1=
fullname1\u end
等的
位置

然后,您可以使用
command\u end-command\u start
查找令牌的长度,例如,
strncpy()
数组的长度等等

所以你会得到这样的结果(我使用了非常冗长的名字来避免评论):

输出:

command: add
fullname1: Walburga Granger
另一种方法是对输入进行迭代,试图在过程中找到这些键字符,如:

int main(void)
{   
    char input[] = "add Walburga Granger [f] mother Sirius Black [m]";
    char command[30];
    char name1[30];
    char gender1[4];
    char rel[30];
    char name2[30];
    char gender2[4];

    int i = 0;
    int j = 0;

    // extract command
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == ' ')
            break;
        command[j] = input[i];
    }
    command[j] = '\0';
    i++;

    // extract first fullname1
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == '[')
            break;
        name1[j] = input[i];
    }
    name1[j - 1] = '\0';

    // extract gender1
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == ' ')
            break;
        gender1[j] = input[i];
    }
    gender1[j] = '\0';

and so on....

这段代码上的缩进是恶性的。你能把它清理一下吗?你需要一次从用户那里得到完整的字符串吗?而不是先问命令,然后问姓名,然后问性别等等?一次从用户那里得到完整的字符串。因此,添加,但它不适用于
add Walburga[f]母亲Sirius Potter Black[m]
添加沃尔伯格·格兰杰[f]天狼星母亲[m]
正是如此,如果可能的话,我需要一个通用的解决方案。@АаааааСјачаааСаааачћћаћћ我添加了第三种方法来挽救您的大部分代码。它会重新。
int main(void)
{   
    char input[] = "add Walburga Granger [f] mother Sirius Black [m]";
    char command[30];
    char name1[30];
    char gender1[4];
    char rel[30];
    char name2[30];
    char gender2[4];

    int i = 0;
    int j = 0;

    // extract command
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == ' ')
            break;
        command[j] = input[i];
    }
    command[j] = '\0';
    i++;

    // extract first fullname1
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == '[')
            break;
        name1[j] = input[i];
    }
    name1[j - 1] = '\0';

    // extract gender1
    for (j = 0; i < strlen(input); i++, j++)
    {
        if (input[i] == ' ')
            break;
        gender1[j] = input[i];
    }
    gender1[j] = '\0';

and so on....
char fullname1[100];
char fullname2[100];

// build fullname1 
strcpy(fullname1, commands[1]);
i = 2;
while (commands[i][0] != '[')
{
    strcat(fullname1, " ");
    strcat(fullname1, commands[i]);
    i++;
}

// build fullname2
i += 2;
strcpy(fullname2, commands[i]);
i++;
while (commands[i][0] != '[')
{
    strcat(fullname2, " ");
    strcat(fullname2, commands[i]);
    i++;
}

printf("%s\n", fullname1);
printf("%s\n", fullname2);