您可以使用strtok 进行拆分,如下所示:
struct split_result {
int cnt;
char *buf;
char **strs;
};
int
split(const char *str, struct split_result *rst)
{
int idx, str_num;
char *buf, *sep, **strs;
buf = strdup(str);
if (buf == NULL) {
perror("strdup");
return -1;
}
str_num = 1;
strs = malloc(str_num * sizeof(char *));
if (strs == NULL) {
perror("malloc");
return -1;
}
sep = " \t";
idx = 0;
for (strs[idx] = strtok(buf, sep);
strs[idx];
strs[idx] = strtok(NULL, sep))
{
idx++;
if (idx >= str_num) {
str_num += 10;
strs = realloc(strs, str_num * sizeof(char *));
if (strs == NULL) {
perror("realloc");
return -1;
}
}
}
rst->cnt = idx;
rst->strs = strs;
rst->buf = buf;
return 0;
}
这是一个测试程序及其输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct split_result {
int cnt;
char *buf;
char **strs;
};
int
split(const char *str, struct split_result *rst)
{
int idx, str_num;
char *buf, *sep, **strs;
buf = strdup(str);
if (buf == NULL) {
perror("strdup");
return -1;
}
str_num = 1;
strs = malloc(str_num * sizeof(char *));
if (strs == NULL) {
perror("malloc");
return -1;
}
sep = " \t";
idx = 0;
for (strs[idx] = strtok(buf, sep);
strs[idx];
strs[idx] = strtok(NULL, sep))
{
idx++;
if (idx >= str_num) {
str_num += 10;
strs = realloc(strs, str_num * sizeof(char *));
if (strs == NULL) {
perror("realloc");
return -1;
}
}
}
rst->cnt = idx;
rst->strs = strs;
rst->buf = buf;
return 0;
}
int
main(void)
{
int i, j;
struct split_result rst;
const char *msg[] = {
"",
" One",
" One Two",
" One Two Tree ",
"There are books in the library.",
NULL,
};
for (i = 0; msg[i]; i++) {
if (split(msg[i], &rst) < 0) {
exit(EXIT_FAILURE);
}
printf("msg[%d] = >>%s<<\n", i, msg[i]);
for (j = 0; j < rst.cnt; j++) {
printf("cnt = %d: |%s|\n", j, rst.strs[j]);
}
free(rst.strs);
free(rst.buf);
}
exit(EXIT_SUCCESS);
}
输出:
$ ./a.out
msg[0] = >><<
msg[1] = >> One<<
cnt = 0: |One|
msg[2] = >> One Two<<
cnt = 0: |One|
cnt = 1: |Two|
msg[3] = >> One Two Tree <<
cnt = 0: |One|
cnt = 1: |Two|
cnt = 2: |Tree|
msg[4] = >>There are books in the library.<<
cnt = 0: |There|
cnt = 1: |are|
cnt = 2: |books|
cnt = 3: |in|
cnt = 4: |the|
cnt = 5: |library.|