C 拆分字符串
C Splitting Strings
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char *strArray[40];
void parsing(char *string){
int i = 0;
char *token = strtok(string, " ");
while(token != NULL)
{
strcpy(strArray[i], token);
printf("[%s]\n", token);
token = strtok(NULL, " ");
i++;
}
}
int main(int argc, char const *argv[]) {
char *command = "This is my best day ever";
parsing(command); //SPLIT WITH " " put them in an array - etc array[0] = This , array[3] = best
return 0;
}
这是我的代码,有什么简单的方法可以解决吗?顺便说一句,我的代码不起作用。我是 C 语言的新手,我不知道该如何处理:( 帮助
strtok()
实际上修改了提供的参数,因此您不能传递字符串文字并期望它起作用。
您需要有一个可修改的参数才能使其正常工作。
根据 man page
Be cautious when using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
FWIW,任何修改字符串文字的尝试都会调用 undefined behavior.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strArray[40];
void parsing(const char *string){//Original does not change
int i = 0;
strArray[i++] = strdup(string);//make copy to strArray[0]
char *token = strtok(*strArray, " ");
while(token != NULL && i < 40 - 1){
strArray[i++] = token;
//printf("[%s]\n", token);
token = strtok(NULL, " ");
}
strArray[i] = NULL;//Sentinel
}
int main(void){
char *command = "This is my best day ever";
parsing(command);
int i = 1;
while(strArray[i]){
printf("%s\n", strArray[i++]);
}
free(strArray[0]);
return 0;
}
int parsing(char *string){//can be changed
int i = 0;
char *token = strtok(string, " ");
while(token != NULL && i < 40){
strArray[i] = malloc(strlen(token)+1);//Ensure a memory for storing
strcpy(strArray[i], token);
token = strtok(NULL, " ");
i++;
}
return i;//return number of elements
}
int main(void){
char command[] = "This is my best day ever";
int n = parsing(command);
for(int i = 0; i < n; ++i){
printf("%s\n", strArray[i]);
free(strArray[i]);
}
return 0;
}
在你们的帮助下我已经完成了,谢谢大家:)
是我的拆分库=https://goo.gl/27Ex6O
代码不是动态的,如果输入 100 位数字,我猜它会崩溃
我们可以使用带有这些参数的库:
正在解析(输出文件、输入文件、拆分字符)
谢谢
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char *strArray[40];
void parsing(char *string){
int i = 0;
char *token = strtok(string, " ");
while(token != NULL)
{
strcpy(strArray[i], token);
printf("[%s]\n", token);
token = strtok(NULL, " ");
i++;
}
}
int main(int argc, char const *argv[]) {
char *command = "This is my best day ever";
parsing(command); //SPLIT WITH " " put them in an array - etc array[0] = This , array[3] = best
return 0;
}
这是我的代码,有什么简单的方法可以解决吗?顺便说一句,我的代码不起作用。我是 C 语言的新手,我不知道该如何处理:( 帮助
strtok()
实际上修改了提供的参数,因此您不能传递字符串文字并期望它起作用。
您需要有一个可修改的参数才能使其正常工作。
根据 man page
Be cautious when using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
FWIW,任何修改字符串文字的尝试都会调用 undefined behavior.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strArray[40];
void parsing(const char *string){//Original does not change
int i = 0;
strArray[i++] = strdup(string);//make copy to strArray[0]
char *token = strtok(*strArray, " ");
while(token != NULL && i < 40 - 1){
strArray[i++] = token;
//printf("[%s]\n", token);
token = strtok(NULL, " ");
}
strArray[i] = NULL;//Sentinel
}
int main(void){
char *command = "This is my best day ever";
parsing(command);
int i = 1;
while(strArray[i]){
printf("%s\n", strArray[i++]);
}
free(strArray[0]);
return 0;
}
int parsing(char *string){//can be changed
int i = 0;
char *token = strtok(string, " ");
while(token != NULL && i < 40){
strArray[i] = malloc(strlen(token)+1);//Ensure a memory for storing
strcpy(strArray[i], token);
token = strtok(NULL, " ");
i++;
}
return i;//return number of elements
}
int main(void){
char command[] = "This is my best day ever";
int n = parsing(command);
for(int i = 0; i < n; ++i){
printf("%s\n", strArray[i]);
free(strArray[i]);
}
return 0;
}
在你们的帮助下我已经完成了,谢谢大家:)
是我的拆分库=https://goo.gl/27Ex6O
代码不是动态的,如果输入 100 位数字,我猜它会崩溃
我们可以使用带有这些参数的库:
正在解析(输出文件、输入文件、拆分字符)
谢谢