我正在使用 strtok
我的目标是将字符串划分为更小的标记,然后将这些单独的标记分配给字符数组或字符串。这是我的代码:
int main(void)
{
char whole[100] = "Please help me";
char *token;
char individual[100];
token = strtok(whole, " ");
individual = token; //I don't know what code fits in here
}
有人可以帮我吗?
最佳答案
#include <stdio.h>
#include <string.h>
int main(void)
{
char whole[100] = "Please help me";
char *token;
char *individual[50];
int i=0, n;
token = strtok(whole, " ");
while(token != NULL){
individual[i++] = token;//just store pointer or make clone = strdup(token);
token = strtok(NULL, " ");
}
n = i;
for(i = 0; i < n; ++i){
puts(individual[i]);
}
return 0;
}
<小时/>
char whole[100] = "Please help me";
char *token;
char individual[50][100];
int i=0, n;
token = strtok(whole, " ");
while(token != NULL){
strcpy(individual[i++], token);//copy to char array
token = strtok(NULL, " ");
}
n = i;
for(i = 0; i < n; ++i){
puts(individual[i]);
}
关于c - 将字符指针的值赋给字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043059/