Split string with multiple delimiters using strtok in C -


I have problem in dividing the string, the code works below, but only if the string is between " Are there. But I need to split the string even if any white space char strtok () is also required?

Four Inputs [1024]; Four * string [3]; Int i = 0; Fgets (Input, 1024, stdin)! = '\ 0') // Get input {string [0] = strtok (input, ""); // Paras while the first string (string [i]! = NULL) // Other paragraph {{printf ("string [% d] =% s \ n", i, string [i]); I ++; String [i] = strotok (null, ""); }

A simple example shows how many delimiters and potential improvements are used in your code. How to do . See embedded comments for clarification.

Warning about the common pitfalls of strtok () (manual):

This function is your first argument.

These functions can not be used on a continuous wire.

The identity of seamankita byte is lost.

strtok () uses a constant buffer during parsing the function, so this thread is not secure, use strtok_r () if It matters to you.


  #include & lt; Stdio.h & gt; #to & lt include, string.h & gt; Int main (zero) {charge input [1024]; Four * string [256]; // 1) 3 is dangerously small, 256 can keep some time ;-) // You want to allocate dynamically indicators // In a normal, strong case, four seamankita [] = "\ t \ r \ N \ v \ f "; // 2) POSIX WHITESPACE VARIETIC INTO I = 0, J = 0; If fgets (input, sizeof input, stdin)) / 3) fgets () returns zero on error // 4) Hard-coding size instead of better practice / input to use size {string [i] = strtok (input, marginal); // 5) I use clearly to be clear (string [i]! = NULL) {printf ("string [% d] =% s \ n", i, string [i]); I ++; String [i] = strtok (zero, limited); } For (j = 0; j & lt; i; j ++) printf ("% s", string [i]); } Return 0; }  

Comments