strtok函数实现与应用

strtok:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
功能:分解字符串为一组标记串。str为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,str必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在str中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
实例:用strtok来判断ip地址是否合法:ip_strtok.c:

[c-sharp] view plain copy print ?
  1. //ip_strtok.c
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(int argc, char **argv)
  5. {
  6. char temp_buf[100] = {};
  7. char p_temp[100];
  8. char *p=NULL;
  9. char *t = ".";
  10. int m,n,i;
  11. int j=0,s=0;
  12. if(argc!=2)
  13. {
  14. printf("param must 2/n");
  15. return -1;
  16. }
  17. strcpy(temp_buf, argv[1]);
  18. for(i=0; i<strlen(temp_buf);i++)
  19. {
  20. if(temp_buf[i] == *t)j++;
  21. if(temp_buf[i] == *t && (temp_buf[i+1]>='0'&&temp_buf[i+1]<='9'))
  22. {
  23. s++;
  24. }
  25. }
  26. if(j!=3 || j!=s)
  27. {
  28. printf("ip param format error/n");
  29. return -1;
  30. }
  31. p = strtok(temp_buf, t);
  32. while(p!=NULL)
  33. {
  34. strcpy(p_temp, p);
  35. printf("%s /n", p_temp);
  36. for(n=0; n<strlen(p_temp);n++)
  37. {
  38. if(!(p_temp[n]>='0'&&p_temp[n]<='9'))
  39. {
  40. printf("ip param error/n");
  41. return -1;
  42. }
  43. }
  44. m = atoi(p_temp);
  45. if(m>255)
  46. {
  47. printf("ip invalid /n");
  48. return -1;
  49. }
  50. p=strtok(NULL, ".");
  51. printf("p = %s/n",p);
  52. }
  53. printf("ok! ip correct! ip=%s/n", argv[1]);
  54. return 0;
  55. }
//ip_strtok.c #include <stdio.h> #include <string.h> int main(int argc, char **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); 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! ip correct! ip=%s/n", argv[1]); return 0; }

编译运行:

[root@localhost liuxltest]# uname -r
2.6.26
[root@localhost liuxltest]# gcc -Wall ip_strtok.c -o ip_strtok
[root@localhost liuxltest]# ./ip_strtok 172.18.4.255
172
p = 18
18
p = 4
4
p = 255
255
p = (null)
ok! ip correct! ip=172.18.4.255
[root@localhost liuxltest]# ./ip_strtok 172.18.
ip param format error

strtok实现函数:

xl_strtok.c

[c-sharp] view plain copy print ?
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. char *xl_strtok(char *str, const char *delim);
  5. int main(int argc, char **argv)
  6. {
  7. if(argc != 3)
  8. {
  9. printf("param must be 3 /n");
  10. return -1;
  11. }
  12. char *temp;
  13. char *p_temp;
  14. char *p;
  15. temp=(char *)malloc(100);
  16. p_temp=(char *)malloc(100);
  17. strcpy(temp, argv[1]);
  18. strcpy(p_temp, argv[2]);
  19. printf("temp = %s /n",temp);
  20. printf("p_temp = %s /n", p_temp);
  21. p = xl_strtok(temp, p_temp);
  22. printf("%s/n", p);
  23. return 0;
  24. }
  25. char *xl_strtok(char *s, const char *dm)
  26. {
  27. static char *last;
  28. char *tok;
  29. if(s == NULL)
  30. s = last;
  31. if(s == NULL)
  32. return NULL;
  33. tok = s;
  34. while (*s != '/0')
  35. {
  36. while (*dm)
  37. {
  38. if (*s == *dm)
  39. {
  40. *s = '/0';
  41. last = s + 1;
  42. break;
  43. }
  44. s++;
  45. }
  46. }
  47. return tok;
  48. }
#include <string.h> #include <stdio.h> #include <stdlib.h> char *xl_strtok(char *str, const char *delim); int main(int argc, char **argv) { if(argc != 3) { printf("param must be 3 /n"); return -1; } char *temp; char *p_temp; char *p; temp=(char *)malloc(100); p_temp=(char *)malloc(100); strcpy(temp, argv[1]); strcpy(p_temp, argv[2]); printf("temp = %s /n",temp); printf("p_temp = %s /n", p_temp); p = xl_strtok(temp, p_temp); printf("%s/n", p); return 0; } char *xl_strtok(char *s, const char *dm) { static char *last; char *tok; if(s == NULL) s = last; if(s == NULL) return NULL; tok = s; while (*s != '/0') { while (*dm) { if (*s == *dm) { *s = '/0'; last = s + 1; break; } s++; } } return tok; }

strtok源码:

/*********************************************************************/
/***
*strtok.c - tokenize a string with given delimiters
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strtok() - breaks string into series of token
* via repeated calls.
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>
#ifdef _SECURE_VERSION
#include <internal.h>
#else /* _SECURE_VERSION */
#include <mtdll.h>
#endif /* _SECURE_VERSION */

/***
*char *strtok(string, control) - tokenize string with delimiter in control
*
*Purpose:
* strtok considers the string to consist of a sequence of zero or more
* text tokens separated by spans of one or more control chars. the first
* call, with string specified, returns a pointer to the first char of the
* first token, and will write a null char into string immediately
* following the returned token. subsequent calls with zero for the first
* argument (string) will work thru the string until no tokens remain. the
* control string may be different from call to call. when no tokens remain
* in string a NULL pointer is returned. remember the control chars with a
* bit map, one bit per ascii char. the null char is always a control char.
*
*Entry:
* char *string - string to tokenize, or NULL to get next token
* char *control - string of characters to use as delimiters
*
*Exit:
* returns pointer to first token in string, or if string
* was NULL, to next token
* returns NULL when no more tokens remain.
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

#ifdef _SECURE_VERSION
#define _TOKEN *context
#else /* _SECURE_VERSION */
#define _TOKEN ptd->_token
#endif /* _SECURE_VERSION */

#ifdef _SECURE_VERSION
char * __cdecl strtok_s (
char * string,
const char * control,
char ** context
)
#else /* _SECURE_VERSION */
char * __cdecl strtok (
char * string,
const char * control
)
#endif /* _SECURE_VERSION */
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

#ifdef _SECURE_VERSION

/* validation section */
_VALIDATE_RETURN(context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(string != NULL || *context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(control != NULL, EINVAL, NULL);

/* no static storage is needed for the secure version */

#else /* _SECURE_VERSION */

_ptiddata ptd = _getptd();

#endif /* _SECURE_VERSION */

/* Clear control map */
for (count = 0; count < 32; count++)
map[count] = 0;

/* Set bits in delimiter table */
do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

/* Initialize str */

/* If string is NULL, set str to the saved
* pointer (i.e., continue breaking tokens out of the string
* from the last strtok call) */
if (string)
str = string;
else
str = _TOKEN;

/* Find beginning of token (skip over leading delimiters). Note that
* there is no token iff this loop sets str to point to the terminal
* null (*str == '/0') */
while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

/* Find the end of the token. If it is not the end of the string,
* put a null there. */
for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '/0';
break;
}

/* Update nextoken (or the corresponding field in the per-thread data
* structure */
_TOKEN = str;

/* Determine if a token has been found. */
if ( string == str )
return NULL;
else
return string;
}

你可能感兴趣的:(strtok函数实现与应用)