使用 C 分隔成对括号中的内容可能是什么好策略
What might be a good strategy in separating contents in paired parentheses using C
我想实现一个用括号分隔内容的 C 函数。结果存储在一个二维字符数组中,每行存储一个标记。
在 C 中没有正则表达式。所以我想知道可以使用哪些内置函数来实现?
例如,如果输入(* 5 10 (Expression1) 100 (Expression2))
,结果将是一个字符数组,内容为*
、5
、10
、(Expression1)
、100
和 (Expresssion2)
。
好的,根据评论,您的解析选项几乎总是相同的。您可以沿着字符串向下移动指针,手动将标记分隔到数组中,或者使用 strtok
或 strsep
函数。下面的示例使用 strtok
将字符串分隔为 array
(静态声明的指针数组),最多 64
个可用标记的指针(如 #define MAXTOK 64
在开头)。
一个简单的指针 p
和 strlen
用于通过在第二个字符处开始解析并覆盖最后一个 ')'
来跳过打开和关闭 '()'
一个 null-terminating
字符:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTOK 64
int main (void) {
char s[] = "(* 5 10 (Expression1) 100 (Expression2))";
char *array[MAXTOK] = {NULL};
char *p = s;
size_t idx = 0;
size_t len = 0;
size_t i;
len = strlen (s);
p++; /* skip opening '(' */
s[len-1] = 0; /* skip closing ')' */
/* parse remaining string into tokens stored in array */
for (p = strtok (p, " "); p; p = strtok (NULL, " ")) {
array[idx++] = strdup (p); /* allocate mem for tok, assign to array */
if (idx == MAXTOK) { /* check max number of pointers filled */
fprintf (stderr, "warning: max tokens filled.\n");
break; /* if you dynamically declare array, you can realloc */
}
}
for (i = 0; i < idx; i++)
printf (" array[%2zu] = %s\n", i, array[i]);
/* free memory allocated by strdup */
for (i = 0; i < idx; i++)
free (array[i]);
return 0;
}
输出
$ ./bin/parse_expr
array[ 0] = *
array[ 1] = 5
array[ 2] = 10
array[ 3] = (Expression1)
array[ 4] = 100
array[ 5] = (Expression2)
这是标准方法。您的另一个选择是动态声明 char **array;
并在初始时使用 calloc
(或 malloc
)分配指针,并在达到限制时根据需要分配 realloc
。
我想实现一个用括号分隔内容的 C 函数。结果存储在一个二维字符数组中,每行存储一个标记。 在 C 中没有正则表达式。所以我想知道可以使用哪些内置函数来实现?
例如,如果输入(* 5 10 (Expression1) 100 (Expression2))
,结果将是一个字符数组,内容为*
、5
、10
、(Expression1)
、100
和 (Expresssion2)
。
好的,根据评论,您的解析选项几乎总是相同的。您可以沿着字符串向下移动指针,手动将标记分隔到数组中,或者使用 strtok
或 strsep
函数。下面的示例使用 strtok
将字符串分隔为 array
(静态声明的指针数组),最多 64
个可用标记的指针(如 #define MAXTOK 64
在开头)。
一个简单的指针 p
和 strlen
用于通过在第二个字符处开始解析并覆盖最后一个 ')'
来跳过打开和关闭 '()'
一个 null-terminating
字符:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTOK 64
int main (void) {
char s[] = "(* 5 10 (Expression1) 100 (Expression2))";
char *array[MAXTOK] = {NULL};
char *p = s;
size_t idx = 0;
size_t len = 0;
size_t i;
len = strlen (s);
p++; /* skip opening '(' */
s[len-1] = 0; /* skip closing ')' */
/* parse remaining string into tokens stored in array */
for (p = strtok (p, " "); p; p = strtok (NULL, " ")) {
array[idx++] = strdup (p); /* allocate mem for tok, assign to array */
if (idx == MAXTOK) { /* check max number of pointers filled */
fprintf (stderr, "warning: max tokens filled.\n");
break; /* if you dynamically declare array, you can realloc */
}
}
for (i = 0; i < idx; i++)
printf (" array[%2zu] = %s\n", i, array[i]);
/* free memory allocated by strdup */
for (i = 0; i < idx; i++)
free (array[i]);
return 0;
}
输出
$ ./bin/parse_expr
array[ 0] = *
array[ 1] = 5
array[ 2] = 10
array[ 3] = (Expression1)
array[ 4] = 100
array[ 5] = (Expression2)
这是标准方法。您的另一个选择是动态声明 char **array;
并在初始时使用 calloc
(或 malloc
)分配指针,并在达到限制时根据需要分配 realloc
。