0

so I need help separating one string into multiple separate ones. For example, let's say I have something like:

char sentence[]= "This is a sentence.";

and I want to split it to:

char A[]="This";
char B[]="is";
char C[]="a";
char D[]="sentence.";
nidau00
  • 29
  • 3

2 Answers2

1

Same question, different split requirement:

Split string with delimiters in C

Community
  • 1
  • 1
Bowen Yang
  • 146
  • 1
  • 9
0

As mentioned you can use strtok() to do this job. The usage is not very intuitive:

char sentence[]= "This is a sentence."; // sentence is changed during tokenization. If you want to keep the original data, copy it.

char *word = strtok( sentence, " ." ); // Only space + full stop for having a multi delimiter example
while (word!=NULL)
{
  // word points to the first part. The end of the word is marked with \0 in the original string
  // Do something with it, process it, store it
  … 
 word = strtok( NULL, " ."); // To get the next word out of sentence, pass NULL
}
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50