Realization and application of strtok function

Source: Internet
Author: User
Tags strlen strtok

Turn from: http://blog.csdn.net/ast_224/article/details/3977311

Strtok
#include <string.h>
Char *strtok (char *str, const char *delim);
Char *strtok_r (char *str, const char *delim, char **saveptr);
Function: Explode a string into a set of tag strings. STR is the string to be decomposed, Delim as a delimiter string.
Note: At the first invocation, STR must point to the string to be decomposed, and then call to set S to null.
Strtok finds the characters contained in Delim in STR and replaces it with null ('/0 ') until the entire string is searched.
Returns a pointer to the next tag string. Returns the null character null when no string is marked.
Example: Use Strtok to determine whether an IP address is legitimate: ip_strtok.c:

[C-sharp]  View plain copy//ip_strtok.c   #include  <stdio.h>   #include  < string.h>   Int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV)    {       char temp_buf[100] = {};      char p_temp[100];       char *p=NULL;      char *t =  ".";       int m,n,i;      int j=0,s=0;             if (argc!=2)       {         printf ("param must 2/n");        return -1;       }      strcpy (temp_buf, argv[1]);       for (I=0; i<strlen (TEMP_BUF); i++)       {          if (temp_buf[i] == *t) j++;        if (temp_buf[i] = = *t &&  (temp_buf[i+1]>= ' 0 ' &&temp_buf[i+1]<= ' 9 '))          {          s++;         }      }      if (j!=3 | |  j!=s)       {        printf ("Ip param  format error/n ");        return -1;       }      p = strtok (temp_buf, t);             while (p!=null)       {          strcpy (p_temp, p);         printf ("%s /n",  p_temp);    &Nbsp;    for (N=0; n<strlen (p_temp); n++)           {           if ( p_temp[n]>= ' 0 ' &&p_temp[n]<= ' 9 ')            {              printf ("ip param error/n");              return -1;            }         }                   m = atoi (p_temp);          if (m>255)          {            printf ("ip invalid /n");           return -1;         }                   p=strtok (null,  ".");          printf ("p = %s/n", p);      }       printf ("OK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.