logo

Funzioni strtok() e strtok_r() in C con esempi

C fornisce due funzioni strtok() e strtok_r() per dividere una stringa in base a un delimitatore. Dividere una stringa è un compito molto comune. Ad esempio, abbiamo un elenco di elementi separati da virgole da un file e vogliamo singoli elementi in un array.

Funzione strtok()

Il metodo strtok() divide str[] in base ai delimitatori forniti e restituisce il token successivo. Deve essere chiamato in un ciclo per ottenere tutti i token. Restituisce NULL quando non ci sono più token.



Sintassi di strtok()

char * strtok (char * str , const char * delims );>

Parametri

    str: è il puntatore alla stringa da tokenizzare. delims: è una stringa contenente tutti i delimitatori.

Valore di ritorno

  • Restituisce il puntatore al primo token incontrato nella stringa.
  • Restituisce NULL se non sono stati trovati altri token.

Esempi di strtok()

Esempio 1: C Programma per dimostrare come dividere una stringa utilizzando strtok().

C








// C program for splitting a string> // using strtok()> #include> #include> int> main()> {> >char> str[] =>'Geeks-for-Geeks'>;> >// Returns first token> >char>* token =>strtok>(str,>' - '>);> >// Keep printing tokens while one of the> >// delimiters present in str[].> >while> (token != NULL) {> >printf>(>' % s '>, token);> >token =>strtok>(NULL,>' - '>);> >}> >return> 0;> }>

>

>

Produzione

 Geeks for Geeks>

Esempio 2: Programma per dimostrare l'uso della funzione strtok() per tokenizzare una stringa in base a un delimitatore.

C




// C code to demonstrate working of> // strtok> #include> #include> // Driver function> int> main()> {> >// Declaration of string> >char> gfg[100] =>' Geeks - for - geeks - Contribute'>;> >// Declaration of delimiter> >const> char> s[4] =>'-'>;> >char>* tok;> >// Use of strtok> >// get first token> >tok =>strtok>(gfg, s);> >// Checks for delimiter> >while> (tok != 0) {> >printf>(>' %s '>, tok);> >// Use of strtok> >// go through other tokens> >tok =>strtok>(0, s);> >}> >return> (0);> }>

>

>

Produzione

 Geeks for geeks Contribute>

Applicazione pratica di strtok()

strtok() può essere utilizzato per dividere una stringa in più stringhe in base ad alcuni separatori. UN semplice file CSV il supporto potrebbe essere implementato utilizzando questa funzione. I file CSV hanno virgole come delimitatori.

Esempio 3: C Programma per dimostrare l'uso della funzione strtok() in C per implementare a semplice file CSV.

C




// C code to demonstrate practical application of> // strtok> #include> #include> // Driver function> int> main()> {> >// Declaration of string> >// Information to be converted into CSV file> >char> gfg[100] =>' 1997 Ford E350 ac 3000.00'>;> >// Declaration of delimiter> >const> char> s[4] =>' '>;> >char>* tok;> >// Use of strtok> >// get first token> >tok =>strtok>(gfg, s);> >// Checks for delimiter> >while> (tok != 0) {> >printf>(>'%s, '>, tok);> >// Use of strtok> >// go through other tokens> >tok =>strtok>(0, s);> >}> >return> (0);> }>

>

>

Produzione

1997, Ford, E350, ac, 3000.00,>

Funzione strtok_r()

Proprio come la funzione strtok() in C, strtok_r() svolge lo stesso compito di analizzare una stringa in una sequenza di token. strtok_r() è una versione rientrante di strtok(), quindi è thread-safe.

Sintassi di strtok_r()

char * strtok_r (char * str , const char * delim , char ** saveptr );>

Parametri

    str: è il puntatore alla stringa da tokenizzare. delims: è una stringa contenente tutti i delimitatori. saveptr : è un puntatore a una variabile char * utilizzata internamente da strtok_r() per mantenere il contesto tra chiamate successive che analizzano la stessa stringa.

Valore di ritorno

  • Restituisce il puntatore al primo token incontrato nella stringa.
  • Restituisce NULL se non sono stati trovati altri token.

Esempi di strtok_r()

Esempio 1: un semplice programma C per mostrare l'uso di strtok_r().

C




// C program to demonstrate working of strtok_r()> // by splitting string based on space character.> #include> #include> int> main()> {> >char> str[] =>'Geeks for Geeks'>;> >char>* token;> >char>* rest = str;> >while> ((token = strtok_r(rest,>' '>, &rest)))> >printf>(>'%s '>, token);> >return> (0);> }>

>

>

Produzione

Geeks for Geeks>

Tokenizzazione annidata in C

Esempio 2: Il programma C seguente dimostra l'uso della funzione strtok_r() per la tokenizzazione nidificata.

C




#include> #include> int> main()> {> >char> str[] =>'Hello, World! Geeks for Geeks.'>;> >const> char> outer_delimiters[] =>'!.'>;> >const> char> inner_delimiters[] =>' ,'>;> >char>* token;> >char>* outer_saveptr = NULL;> >char>* inner_saveptr = NULL;> >token = strtok_r(str, outer_delimiters, &outer_saveptr);> >while> (token != NULL) {> >printf>(>'Outer Token: %s '>, token);> >char>* inner_token = strtok_r(> >token, inner_delimiters, &inner_saveptr);> >while> (inner_token != NULL) {> >printf>(>'Inner Token: %s '>, inner_token);> >inner_token = strtok_r(NULL, inner_delimiters,> >&inner_saveptr);> >}> >token = strtok_r(NULL, outer_delimiters,> >&outer_saveptr);> >}> >return> 0;> }>

>

>

Produzione

Outer Token: Hello, World Inner Token: Hello Inner Token: World Outer Token: Geeks for Geeks Inner Token: Geeks Inner Token: for Inner Token: Geeks>

Differenza tra strtok() e strtok_r()

Vediamo le differenze tra le funzioni strtok() e strtok_r() in forma tabellare come mostrato di seguito:

Si No.

strtok()

strtok_r()

1.

Viene utilizzato per suddividere la stringa str in una serie di token. Viene utilizzato per decodificare una stringa in un modello per i token.

2.

La sintassi è la seguente:

char *strtok(char *str, const char *delim)

età soleggiata e soleggiata
La sua sintassi è la seguente:
char *strtok_r(char *string, const char *limiter, char **context);

3.

Utilizza il delimitatore per procedere. È una variante reinserita di strtok().

4.

Richiede due parametri. Ci vogliono tre parametri.

5.

Restituisce un puntatore al primo token trovato nella stringa. Restituisce un puntatore al primo token trovato nella stringa.
6. Non è thread-safe. È thread-safe.