英文:
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 " ,"
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
.
答案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 < 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);
}
答案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 = "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 contains charachters from the previous delimiters, so you can not use 'o' as one because the delimiter "or" contains 'o'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论