Latest web development tutorials

funzioni di libreria C - strtok ()

C libreria standard - <string.h> C libreria standard - <string.h>

descrizione

funzioni di libreria Cchar * strtok (char * str, const char * delim) scomposto in una serie di stringa str stringa,delim come delimitatore.

dichiarazione

Ecco () funzione di dichiarazione strtok.

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

parametri

  • str - per essere suddiviso in una serie di stringhe di piccole stringhe.
  • delim - C stringa contenente il separatore.

Valore di ritorno

Questa funzione restituisce l'ultima stringa decomposto, se nessun testo da cercare, restituisce un puntatore nullo.

Esempi

L'esempio seguente mostra viene utilizzata la funzione strtok ().

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

int main()
{
   const char str[80] = "This is - www.w3cschool.cc - website";
   const char s[2] = "-";
   char *token;
   
   /* 获取第一个子字符串 */
   token = strtok(str, s);
   
   /* 继续获取其他的子字符串 */
   while( token != NULL ) 
   {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

Facciamo compilare ed eseguire il programma di cui sopra, che si tradurrà in quanto segue:

This is 
www.w3cschool.cc 
website

C libreria standard - <string.h> C libreria standard - <string.h>