"C" 用分隔符 space 拆分字符串,但在某些单词(姓氏、名字)之间转义 space
"C" split string by space with delimiter but escape space between certain words (last, first name)
我想检查来自用户的输入命令
例如:
add person relation person (person can be for example John, or "John Smith")
示例:add John Smith brother Jack Smith
...
我使用定界符 (space) 将字符串拆分为多个。字符串,但我必须将人名保留为一个 string/parameter(人名和姓氏之间可以是 1 space 或更多,在每种情况下我都必须将其解释为一个参数)。
// input from cmd stored in "input" variable with fgets() in main function...
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;
在我的代码中,我按 space 拆分字符串,但是如何忽略名称之间的 space?
或者说,将 "add" 和 "relation" 之间的字符串存储在一个字符串中,在 "relation" 之后也存储在一个字符串中...
使用strchr
你可以遍历字符串,解析命令名称性别关系性别。如果单词之间有多个 space,这将失败,但可以添加额外的逻辑以容纳多个 space。如果未观察到格式 command name relation name
,它也会失败,但可以再次添加额外的逻辑来处理该问题。
需要添加检查以处理 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] = '[=10=]';
}
//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] = '[=10=]';
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] = '[=10=]';
}
//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] = '[=10=]';
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] = '[=10=]';
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;
}
我想检查来自用户的输入命令 例如:
add person relation person (person can be for example John, or "John Smith")
示例:add John Smith brother Jack Smith
...
我使用定界符 (space) 将字符串拆分为多个。字符串,但我必须将人名保留为一个 string/parameter(人名和姓氏之间可以是 1 space 或更多,在每种情况下我都必须将其解释为一个参数)。
// input from cmd stored in "input" variable with fgets() in main function...
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;
在我的代码中,我按 space 拆分字符串,但是如何忽略名称之间的 space? 或者说,将 "add" 和 "relation" 之间的字符串存储在一个字符串中,在 "relation" 之后也存储在一个字符串中...
使用strchr
你可以遍历字符串,解析命令名称性别关系性别。如果单词之间有多个 space,这将失败,但可以添加额外的逻辑以容纳多个 space。如果未观察到格式 command name relation name
,它也会失败,但可以再次添加额外的逻辑来处理该问题。
需要添加检查以处理 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] = '[=10=]';
}
//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] = '[=10=]';
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] = '[=10=]';
}
//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] = '[=10=]';
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] = '[=10=]';
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;
}