Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with multiple delimiters using strtok in C

Tags:

c

string

split

I have problem with splitting a string. The code below works, but only if between strings are ' ' (spaces). But I need to split strings even if there is any whitespace char. Is strtok() even necessary?

char input[1024];
char *string[3];           
int i=0;

fgets(input,1024,stdin)!='\0')               //get input
{                                        
  string[0]=strtok(input," ");               //parce first string
  while(string[i]!=NULL)                     //parce others
  {
     printf("string [%d]=%s\n",i,string[i]);
     i++;
     string[i]=strtok(NULL," ");
  }
like image 521
nocturne Avatar asked Oct 27 '14 22:10

nocturne


People also ask

Can you use strtok with multiple delimiters?

The argument to strtok can contain as many separators as you want.

Why multiple call to strtok () is not safe?

The strtok() function uses a static buffer while parsing, so it's not thread safe.

What is strtok () in C?

The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.

Can you use strtok on string?

strtok() splits a string ( string ) into smaller strings (tokens), with each token being delimited by any character from token . That is, if you have a string like "This is an example string" you could tokenize this string into its individual words by using the space character as the token .


1 Answers

A simple example that shows how to use multiple delimiters and potential improvements in your code. See embedded comments for explanation.

Be warned about the general shortcomings of strtok() (from manual):

These functions modify their first argument.

These functions cannot be used on constant strings.

The identity of the delimiting byte is lost.

The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.


#include <stdio.h>
#include<string.h>

int main(void)
{    
  char input[1024];
  char *string[256];            // 1) 3 is dangerously small,256 can hold a while;-) 
                                // You may want to dynamically allocate the pointers
                                // in a general, robust case. 
  char delimit[]=" \t\r\n\v\f"; // 2) POSIX whitespace characters
  int i = 0, j = 0;

  if(fgets(input, sizeof input, stdin)) // 3) fgets() returns NULL on error.
                                        // 4) Better practice to use sizeof 
                                        //    input rather hard-coding size 
  {                                        
    string[i]=strtok(input,delimit);    // 5) Make use of i to be explicit 
    while(string[i]!=NULL)                    
    {
      printf("string [%d]=%s\n",i,string[i]);
      i++;
      string[i]=strtok(NULL,delimit);
    }

    for (j=0;j<i;j++)
    printf("%s", string[i]);
  }

  return 0;
}
like image 118
P.P Avatar answered Sep 28 '22 11:09

P.P