“C”按空格分隔字符串和分隔符,但在某些单词之间转义空格(最后一个名字)

时间:2016-12-07 13:08:37

标签: c delimiter

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

add person relation person (person can be for example John, or "John Smith")

示例:add John Smith brother Jack Smith ...

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

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

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

1 个答案:

答案 0 :(得分:0)

使用strchr,您可以遍历字符串,解析命令名称性别关系性。如果单词之间有多个空格,则会失败,但可以添加额外的逻辑以容纳多个空格。如果没有观察到格式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] = '\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;
}