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

时间:2016-12-09 18:44:25

标签: c

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

2 个答案:

答案 0 :(得分:1)

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

command fullname1 [gender] relation fullname2 [gender]

我建议使用strchr()找到第一个空格并将其命名为command_end。然后找到第一个[并致电position of [ - 1 = fullname1_end等。

然后您会找到令牌的长度,例如command_end - command_startstrncpy()长度到数组等等。

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

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

    char* command_start = input;
    char* command_end = strchr(input, ' ');   // find the first whitespace of input
    char* fullname1_start = command_end + 1;  
    char* gender1_start = strchr(input, '[');  // find the first '[' of input
    char* fullname1_end = gender1_start - 1;
    char* gender1_end = strchr(gender1_start, ' ');  // find the first space after gender1 and so on...
    char* rel_start = gender1_end + 1;
    char* rel_end = strchr(rel_start, ' ');
    char* fullname2_start = rel_end + 1;
    char* gender2_start = strchr(fullname2_start, '[');
    char* gender2_end = strchr(gender2_start, '\0');
    char* fullname2_end = gender2_start - 1;

    int command_length = command_end - command_start;
    strncpy(command, command_start, command_length);
    command[command_length] = '\0';

    int fullname1_length = fullname1_end - fullname1_start;
    strncpy(fullname1, fullname1_start, fullname1_length);
    fullname1[fullname1_length] = '\0';

    printf("command: %s\n", command);
    printf("fullname1: %s\n", fullname1);
}

输出:

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....

挽救大部分代码的第三种方法。在获得命令令牌后,您将插入此片段。

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);

答案 1 :(得分:0)

我认为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" */