[英]Implementing a C function that splits a string on a given character and returns an array of strings after the split (along with length of array)
I'm trying to implement a C function that takes a string and then breaks that string on a certain character and returns back an array of strings after the split along with the size of that array.我正在尝试实现一个 C 函数,该函数接受一个字符串,然后在某个字符上断开该字符串,并在拆分后返回一个字符串数组以及该数组的大小。 I'm using a data structure for this since returning a 2D array (the array of strings after the split) and its length is not possible.
我为此使用了一个数据结构,因为返回一个二维数组(拆分后的字符串数组)并且它的长度是不可能的。 My code is given below:
我的代码如下:
struct charArr {
char *arr[10000];
int size;
};
struct charArr *stringSplitter(char *str, char c) {
struct charArr *splitString = (struct charArr *)malloc(sizeof(struct charArr));
if (splitString == NULL) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
splitString->size = 0;
int i = 0;
int j = 0;
while (str[i] != '\0') {
if (str[i] == c) {
splitString->arr[splitString->size][j] = '\0';
(splitString->size)++;
j = 0;
i++;
while (str[i] == c) { /* this loop is to ignore continuous occurrences of the character c */
i++;
}
} else {
splitString->arr[splitString->size][j] = str[i];
i++;
j++;
}
}
splitString->arr[splitString->size][j] = '\0';
return splitString;
}
int main(int argc, char *argv[]) {
// take input from command line
if (argc == 1) {
//buffer to store lines
size_t buffer_size = 128;
char *buffer = malloc(buffer_size * sizeof(char));
if (buffer == NULL) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
// loop continuously till user exits by ctrl+c
while (1) {
printf("Enter Input> ");
getline(&buffer, &buffer_size, stdin);
char *str = strdup(buffer);
struct charArr *splitString = stringSplitter(str, '&');
for (int i = 0; i<splitString->size; i++) {
printf("%s ", splitString->arr[i]);
}
}
}
return 0;
}
On running the code on a simple input like (the input is continuously taken from the command line):在一个简单的输入上运行代码(输入是从命令行连续获取的):
Enter Input> this & that
输入输入>这个和那个
I expect the output to be:我希望输出是:
this that
这个那个
But, I'm getting the error:但是,我收到错误:
Segmentation fault (core dumped)
分段错误(核心转储)
If the input is as shown below (ie; continuous occurrences of the splitting character):如果输入如下图(即分割字符连续出现):
Enter Input> this &&& that
输入 Input> this &&& that
then also the output must be:那么输出也必须是:
this that
这个那个
Edit: I'm trying to extend this to split a string on multiple delimiters as well (in one go), so instead of char c in the above function, if char *c is passed which is a string of delimiters (example c = " \\t\\n" to remove all white spaces from given string), then also it should work as expected and return an array of strings after the split and length of array.编辑:我试图扩展这个分割在多个分隔符的字符串,以及(在一个GO),因此,而不是在上述功能炭C,如果字符* c的传递是定界符的一个字符串(例如,c = " \\t\\n"从给定的字符串中删除所有空格),然后它也应该按预期工作并在拆分和数组长度后返回一个字符串数组。 For example, if input is (multiple spaces, tabs and newline):
例如,如果输入是(多个空格、制表符和换行符):
Enter Input> this that
Then the array returned (which is a part of the returned structure) must be of size 2 and only contain the 2 strings - "this" and "that".然后返回的数组(它是返回结构的一部分)的大小必须为 2,并且只包含 2 个字符串 - “this”和“that”。
Here's a rewrite of your function with the corrections that you need for proper allocation of each found string using strdup():这是您的函数的重写,其中包含使用 strdup() 正确分配每个找到的字符串所需的更正:
You can find my modifications preceded with comments that start 'Previously':您可以在以“以前”开头的评论之前找到我的修改:
struct charArr* stringSplitter(char *str, char c){
struct charArr* splitString = (struct charArr*)malloc(sizeof(struct charArr));
char buffer[ MAX_BUFF ] ;
if(splitString == NULL){
fprintf(stderr, "malloc failed\n");
exit(1);
}
splitString->size = 0;
int i=0;
int j=0;
while(str[i] != '\0'){
if(str[i] == c){
//Previously: splitString->arr[splitString->size][j] = '\0';
splitString->arr[splitString->size] = strndup( buffer , j );
(splitString->size)++;
j = 0;
i++;
while(str[i] == c){ /* this loop is to ignore continuous occurrences of the character c */
i++;
}
} else {
// Previously: splitString->arr[splitString->size][j] = str[i];
buffer[j] = str[i];
i++;
j++;
}
}
//Previously: splitString->arr[splitString->size][j] = '\0';
splitString->arr[splitString->size++] = strndup( buffer , j );
return splitString;
}
It's been a long time since I wrote any C so I thought this would be a challenge.我已经很久没有写任何 C 语言了,所以我认为这将是一个挑战。 Here's a rewrite of the stringSplitter function.
这是 stringSplitter 函数的重写。
struct charArr* stringSplitter(char *str, char c){
struct charArr* splitString = (struct charArr*)malloc(sizeof(struct charArr));
if(splitString == NULL){
fprintf(stderr, "malloc failed\n");
exit(1);
}
splitString->size = 0;
char sep[2];
sep[0] = c;
sep[1] = (char) 0;
char* next;
while( (next = strtok( str, sep )) )
{
str = NULL;
splitString->arr[ splitString->size++ ] = next;
}
return splitString;
}
Above, I'm simply using strtok
.上面,我只是使用
strtok
。 Take a look at the manpage for strtok() to see it's nuances.查看 strtok() 的联机帮助页以了解其细微差别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.