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

标签 c

我的任务是制作家庭二叉树。在此任务中,我需要执行一些命令(例如添加、绘制、列表..)首先我需要标记标准输入。例如“add Walburga [f] mother Sirius Black [m]”)以查找哪些命令,输入名称、姓氏、关系和性别。 我已经用 strtok 函数完成了这个,现在我有了数组,它包含来自标准输入的每个单独的字符串作为参数。但现在我需要将名字和姓氏(如果存在)连接为一个参数。

最佳答案

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

command fullname1 [gender] relation fullname2 [gender]

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

然后您可以使用 command_end - command_start 找到 token 的长度,例如使用 strncpy() 找到数组的长度等等。

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

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

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

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

关于c - 将字符串解析为参数,然后连接其中的一些参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066927/

相关文章:

我可以使用 GCC 编译一个自动要求管理员权限的 C 程序吗?

使用 strcpy 将输入的字符串复制到字符串数组的 "ith"元素中

c - C 中的按名称传递实现

luaL_loadfile()可以用luaL_loadbuffer()代替吗?

检查是否 errno != EINTR : what does it mean?

c - 手动输入时,Poll 在 stdin 上工作,但在输入通过管道传输且未重定向时不工作

c++ - 何时确切分配堆栈

java - libusb 与 USB 大容量存储器通信

c - 在 C 中将 func(const void *) 赋值给 func(void *)

c++ - 使用尾递归实现 Tak 函数