gpt4 book ai didi

c - "C"用分隔符按空格分割字符串,但在某些单词之间转义空格(姓氏、名字)

转载 作者:行者123 更新时间:2023-11-30 19:36:20 26 4
gpt4 key购买 nike

我想检查用户输入的命令如:

添加人员关系人员(人员可以是例如 John 或“John Smith”)

示例:添加 John Smith 兄弟 Jack Smith...

我使用分隔符(空格)将字符串拆分为多个。字符串,但我必须将人名保留为一个字符串/参数(人名和姓氏之间可以有 1 个空格或更多,在每种情况下我都必须将其解释为一个参数)。

//cmd 的输入存储在主函数中的 fgets() 的“input”变量中...

char inputTerminalCommands(char *input) // takes char input from main, splits strings and then should compare type of command from user
{

int i = 0;
char *str = input;
char *split = strtok(str, " "); // split string into words after "space"

char *array[6];

while(split!= 0)
{
array[i++] = split;
split = strtok(NULL, " ");
}

return 0;

在我的代码中,我按空格分割字符串,但如何忽略名称之间的空格?或者说,将“add”和“relation”之间的字符串存储在一个字符串中,并将“relation”之后的字符串也存储在一个字符串中...

最佳答案

使用strchr您可以迭代字符串,解析命令名称性别关系sex。如果单词之间有多个空格,则此操作将会失败,但可以添加额外的逻辑来容纳多个空格。如果未遵守格式命令名称关系名称,它也会失败,但可以再次添加额外的逻辑来处理该问题。
需要添加检查来处理 malloc 失败。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct parse {
char *command;
char *name;
char gender;
char *relation;
char *identity;
char sex;
};


int main(void)
{
//char input[] = "add John Smith III [m] brother David William Smith [m]";
char input[] = "add John Smith III brother David William Smith";
char *start = NULL;
char *stop = NULL;
int span = 0;
struct parse item;
//parse command - one word
start = input;
if ( ( stop = strchr ( start, ' '))) {
span = stop - start;
item.command = malloc ( span + 1);
memmove ( item.command, start, span);
item.command[span] = '\0';
}
//parse name - could be many words, each begins with upper case
start = stop + 1;
stop = start;;
while ( ( stop = strchr ( stop, ' '))) {
if ( !isupper( *(stop + 1))) {
span = stop - start;
item.name = malloc ( span + 1);
memmove ( item.name, start, span);
item.name[span] = '\0';
break;
}
stop = stop + 1;
}
//optional - parse gender - one character in []
start = stop;
item.gender = '*';
if ( *(start + 1) == '[') {
item.gender = *(start + 2);
stop = strchr ( start + 1, ' ');
}
//parse relation - one word
start = stop + 1;
if ( ( stop = strchr ( start, ' '))) {
span = stop - start;
item.relation = malloc ( span + 1);
memmove ( item.relation, start, span);
item.relation[span] = '\0';
}
//parse name - could be many words, each begins with upper case
start = stop + 1;
stop = start;;
while ( ( stop = strchr ( stop, ' '))) {
if ( !isupper( *(stop + 1))) {
span = stop - start;
item.identity = malloc ( span + 1);
memmove ( item.identity, start, span);
item.identity[span] = '\0';
break;
}
stop = stop + 1;
}
if ( !stop) { // if nothing follows identity...strchr failed on last name
span = strlen ( start);
item.identity = malloc ( span + 1);
memmove ( item.identity, start, span);
item.identity[span] = '\0';
stop = start + span;
}
//optional - parse sex - one character in []
start = stop;
item.sex = '*';
if ( *(start + 1) == '[') {
item.sex = *(start + 2);
}

printf ( "%s\n", item.command);
printf ( "%s\n", item.name);
printf ( "%c\n", item.gender);
printf ( "%s\n", item.relation);
printf ( "%s\n", item.identity);
printf ( "%c\n", item.sex);

free ( item.command);
free ( item.name);
free ( item.relation);
free ( item.identity);

return 0;
}

关于c - "C"用分隔符按空格分割字符串,但在某些单词之间转义空格(姓氏、名字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41018367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com