在C中使用多个分隔符拆分字符串。

huangapple go评论57阅读模式
英文:

C - split string using multiple delimiters in C

问题

怎样编写一个函数来使用多个分隔符拆分字符串,这些分隔符可以是字符,比如',',也可以是字符串,比如"or"。

例如,对于字符串char * string = "a or b or c, 8",我希望输出:

a
b
c
8
英文:

How to write a function that split a string using multiples delimiters, which could be char like ',' and could be string like "or".

For example, for the string char * string = "a or b or c, 8", I would like to print :

a 
b
c
8

答案1

得分: 3

I suggest that you first use strtok with the delimiter string " ," and then filter out tokens which have a special meaning, such as "or".

Here is an example:

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

int main( void )
{
    char string[] = "a or b or c, 8";
    const char *delim = " ,"; 
    char *token;

    token = strtok( string, delim );

    while ( token != NULL )
    {
        if ( strcmp( token, "or" ) != 0 )
        {
            printf( "%s\n", token );
        }

        token = strtok( NULL, delim );
    }
}

This program has the following output:

a
b
c
8

Note that strtok requires that the string is writable, because it writes null characters into the string. Therefore, you cannot use a string literal such as

char *string = "a or b or c, 8";

That is why I am using a writable array instead:

char string[] = "a or b or c, 8";

If you want your code to also work on string literals, then you cannot use strtok, but will have to use other functions instead, such as strpbrk.

英文:

I suggest that you first use strtok with the delimiter string &quot; ,&quot; and then filter out tokens which have a special meaning, such as &quot;or&quot;.

Here is an example:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main( void )
{
    char string[] = &quot;a or b or c, 8&quot;;
    const char *delim = &quot; ,&quot;; 
    char *token;

    token = strtok( string, delim );

    while ( token != NULL )
    {
        if ( strcmp( token, &quot;or&quot; ) != 0 )
        {
            printf( &quot;%s\n&quot;, token );
        }

        token = strtok( NULL, delim );
    }
}

This program has the following output:

a
b
c
8

Note that strtok requires that the string is writable, because it writes null characters into the string. Therefore, you cannot use a string literal such as

char *string = &quot;a or b or c, 8&quot;;

That is why I am using a writable array instead:

char string[] = &quot;a or b or c, 8&quot;;

If you want your code to also work on string literals, then you cannot use strtok, but will have to use other functions instead, such as strpbrk.

答案2

得分: -1

以下是您提供的代码的翻译部分:

void printsubstr(const char *start, const char *end, int printEmpty)
{
    if (printEmpty || start < end)
    {
        printf("\"");
        while (start < end && *start)
            printf("%c", *start++);
        printf("\"\n");
    }
}

void print(const char *str, const char *delim, int printEmpty)
{
    const char *found;
    size_t delimlen = strlen(delim);

    while ((found = strstr(str, delim)))
    {
        printsubstr(str, found, printEmpty);
        str = found + delimlen;
    }
    printsubstr(str, str + strlen(str), printEmpty);
}

int main(void)
{
    print("ormirror razor error", "or", 1);
    printf("-----------------------\n");
    print("ormirror razor error", "or", 0);
}
英文:
void printsubstr(const char *start, const char *end, int printEmpty)
{
    if(printEmpty || start &lt; end)
    {
        printf(&quot;\&quot;&quot;);
        while(start &lt; end &amp;&amp; *start) printf(&quot;%c&quot;, *start++);
        printf(&quot;\&quot;\n&quot;);
    }
}

void print(const char *str, const char *delim, int printEmpty)
{
    const char *found;
    size_t delimlen = strlen(delim);

    while((found = strstr(str, delim)))
    {
        printsubstr(str, found, printEmpty);
        str = found + delimlen;
    }
    printsubstr(str, str + strlen(str), printEmpty);
}

int main(void)
{
    print( &quot;ormirror razor error&quot;, &quot;or&quot;, 1);
    printf(&quot;-----------------------\n&quot;);
    print( &quot;ormirror razor error&quot;, &quot;or&quot;, 0);
}

答案3

得分: -3

You can use a simple function like this to split a string by multiple delimiters:

void split_and_or(char *str) {
    const char *delim = "and or";
    char *token;
    token = strtok(str, delim);
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, delim);
    }
}

and in main:

int main() {
    char str[] = "a or b and c or d";
    split_and_or(str);
    return 0;
}

However, this code only works for strings that do not contain characters from the previous delimiters, so you cannot use 'o' as one because the delimiter "or" contains 'o'.

英文:

You can use a simple function like this to split a string by multiple delimiters:

void split_and_or(char *str) {
    const char *delim = &quot;and or&quot;;
    char *token;
    token = strtok(str, delim);
    while (token != NULL) {
        printf(&quot;%s\n&quot;, token);
        token = strtok(NULL, delim);
    }}

and in main:

int main() {
    char str[] = &quot;a or b and c or d&quot;;
    split_and_or(str);
    return 0;                                                                       
}

However this code only works for Strings that do not contains charachters from the previous delimiters, so you can not use 'o' as one because the delimiter "or" contains 'o'.

huangapple
  • 本文由 发表于 2023年5月11日 07:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定