I'm trying to create word generator in C and found Segmentation Fault message.
gdb output :
_GI___strtok_r ( s=0x562d88201188 "some text without comma", delim=0x562d8820117f " ", save_ptr=0x7f570a47aa68 <olds>) at strtok_r.c:72
code with strtox function :
char **words = malloc(sizeof(char *) * NUM_WORDS); int num_words = 0; char *save_ptr; char *word = strtok(text, " "); while (word != NULL) { // Strip leading and trailing whitespace while (isspace(*word)) { word ; } int len = strlen(word); while (len > 0 && isspace(word[len - 1])) { len--; } // Allocate memory for the word and copy it using strdup() words[num_words] = strdup(word); // Move to the next word num_words ; word = strtok(NULL, " "); }
how to use function with an indeterminate number of words in text?
CodePudding user response:
Can't believe someone finally asked for this!
You may want to add verification that realloc()
hasn't returned a NULL.
In brief, the string is chopped on the delimiters provided to strtok()
while realloc()
is used to grow an array of pointers to each of those segments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char buf[] = "Once upon a time there lived a beautiful princess.", *p = buf;
char **t = NULL; size_t sz = sizeof *t;
int n = 0;
while(!!(t=realloc(t,(n 1)*sz))&&!!(t[n]=strtok(p," .\n"))) p=NULL, n ;
for( int i = 0; i < n; i )
puts( t[i] );
free( t );
return 0;
}
Once
upon
a
time
there
lived
a
beautiful
princess
EDIT
Then there is the extension that can handle multiple input lines:
int main() {
char *buf[] = { "Once upon a time\n", "there lived\n", " a beautiful princess.\n" };
char **t = NULL; size_t sz = sizeof *t;
int n = 0;
for( int ln = 0; ln < sizeof buf/sizeof buf[0]; ln ) {
char *p = buf[ln];
while(!!(t=realloc(t,(n 1)*sz))&&!!(t[n]=strtok(p," .\n"))) p=NULL, n ;
}
for( int i = 0; i < n; i )
puts( t[i] );
free( t );
return 0;
}
/* Output same as shown above */
Put the strtok()
as the parameter to strdup()
and you've got yourself something that will preserve words while using a single line input buffer.