拆分数组中的字符串并将其放入C中字符串数组的最简单方法
在C中,最简单的分割数组中的字符串并将其放入字符串数组的方法是什么 比如说拆分数组中的字符串并将其放入C中字符串数组的最简单方法,c,arrays,string,C,Arrays,String,在C中,最简单的分割数组中的字符串并将其放入字符串数组的方法是什么 比如说 ["this is a test", "this is also a test"] 进入 使用C库中的strtok函数。函数将字符串拆分为一系列标记 使用C库中的strtok函数。函数将字符串拆分为一系列标记 使用C库中的strtok函数。函数将字符串拆分为一系列标记 使用C库中的strtok函数。函数将字符串拆分为一系列标记 #包括 #包括 #包括 #包括 字符**拆分(常量字符*s); 内部主(空){ const
["this is a test", "this is also a test"]
进入
使用C库中的
strtok
函数。函数将字符串拆分为一系列标记
使用C库中的
strtok
函数。函数将字符串拆分为一系列标记
使用C库中的
strtok
函数。函数将字符串拆分为一系列标记
使用C库中的
strtok
函数。函数将字符串拆分为一系列标记
#包括
#包括
#包括
#包括
字符**拆分(常量字符*s);
内部主(空){
const char*org[]={“这是一个测试”,“这也是一个测试”};
字符**分解[sizeof(组织)/sizeof(*组织)];
int i,j,n=sizeof(组织)/sizeof(*组织);
对于(i=0;i
#包括
#包括
#包括
#包括
字符**拆分(常量字符*s);
内部主(空){
const char*org[]={“这是一个测试”,“这也是一个测试”};
字符**分解[sizeof(组织)/sizeof(*组织)];
int i,j,n=sizeof(组织)/sizeof(*组织);
对于(i=0;i
#包括
#包括
#包括
#包括
字符**拆分(常量字符*s);
内部主(空){
const char*org[]={“这是一个测试”,“这也是一个测试”};
字符**分解[sizeof(组织)/sizeof(*组织)];
int i,j,n=sizeof(组织)/sizeof(*组织);
对于(i=0;i
#包括
#包括
#包括
#包括
字符**拆分(常量字符*s);
内部主(空){
const char*org[]={“这是一个测试”,“这也是一个测试”};
字符**分解[sizeof(组织)/sizeof(*组织)];
int i,j,n=sizeof(组织)/sizeof(*组织);
对于(i=0;i
基于意见。列出你知道的方法。基于意见。列出你知道的方法。基于意见。列出你知道的方法。基于意见。列出你知道的方法。愿全能的上帝保佑你、父子和圣灵。愿全能的上帝保佑你、父子和圣灵。愿全能的上帝保佑你、父子和圣灵。愿全能的上帝保佑你、父子和圣灵。愿全能的上帝保佑你、父子和圣灵。
[["this", "is", "a", "test"], ["this", "is", "also", "a", "test"]]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char **split(const char *s);
int main(void){
const char *org[] = {"this is a test", "this is also a test"};
char **explode[sizeof(org)/sizeof(*org)];
int i,j, n = sizeof(org)/sizeof(*org);
for(i=0;i < n; ++i){
char **p;
p = explode[i] = split(org[i]);
for(j=0;p[j];++j)//while(*p)
puts(p[j]); // puts(*p++)
printf("\n");
//free(explode[i][0]);//top is clone of original
//free(explode[i]);
}
return 0;
}
static int wordCount(const char *s){
char prev = ' ';
int wc = 0;
while(*s){
if(isspace(prev) && !isspace(*s)){
++wc;
}
prev = *s++;
}
return wc;
}
char **split(const char *s){
int i=0, wc = wordCount(s);
char *word, **result = calloc(wc+1, sizeof(char*));
char *clone = strdup(s);//Note that you are copying a whole
for(word=strtok(clone, " \t\n"); word; word=strtok(NULL, " \t\n")){
result[i++] = word;//or strdup(word); and free(clone); before return
}
return result;
}