Update:
Now, copying and mallocing memory accordingly...
Code:
int main(void)
{
char s1[] = "BCFG -SHRA BR"; // "this is an,example ; "
char s2[] = "FG";
char s3[] = "X";
char *p1, *p2, *p3, *p4, *p5, *p6;
char a[11], b[11], c[11];
int n_char;
/* strtok example/test */
/* Caveat: strtok modifies the incoming string! */
printf("The original string is: %s\n", s1);
n_char = strlen(s1) + 1;
/* Add s1 to the end of s3 */
strncat(s3, s1, n_char);
printf("The new larger string is: %s\n", s3);
p1 = malloc( (sizeof(s3) + 1) * sizeof(char));
p2 = malloc( (sizeof(s3) + 1) * sizeof(char));
p3 = malloc( (sizeof(s3) + 1) * sizeof(char));
p4 = malloc( (sizeof(s3) + 1) * sizeof(char));
p5 = malloc( (sizeof(s3) + 1) * sizeof(char));
p6 = malloc( (sizeof(s3) + 1) * sizeof(char));
strcpy(p1,s3);
strcpy(p2,s3);
strcpy(p3,s3);
strcpy(p4,s3);
strcpy(p5,s3);
strcpy(p6,s3);
p1 = strtok(p1,s2);
// printf("%s\n", strtok(s1, s2));
printf("%s\n", p1);
p2 = strtok(p2, s2);
printf("%s\n", p2);
p3 = strtok(p3, "-");
printf("%s\n", p3);
p4 = strtok(p4, "BC");
printf("%s\n", p4);
p5 = strtok(p5, "Panda");
if(p5 == NULL){
printf("Token not found \n");
}
p6 = strtok(p6, "BC");
printf("%s\n", p6);
return 0;
}
Output is:
The original string is: BCFG -SHRA BR
The new larger string is: XBCFG -SHRA BR
X
X
XBCFG
X
X
Not what I expected at all! I expect
XBC
XBC
XBCFG
X
Token not found
x
Err Hmm