1. Note the characteristics of the string, the end of '\ 0'
2. traversal is the most common, if a query string in another
must distinguish between before and after the issue of who
strtok function for cutting string, the good.
Resolving string is a set of strings. s character is to be decomposed, the delim a delimiter character (if incoming string, each character string passed both delimiter). When the first call, s point to the string to break down, and again after the call should s set to NULL.
Acting on the string s character, to include in the delim as delimiter, a cut into the substring s; if, s is null NULL, the stored pointer SAVE_PTR next call will function as a starting position.
For its use for the following, for example:
#include <string.h>
#include <stdio.h>
int main(void)
{
char input[16] = "abc,d";
char *p;
p = strtok(input, ",");
printf("%s\n", p);
p = strtok(NULL, ",");
printf("%s\n", p);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Operating results:
abc
d
Press the any Key to the Continue
Below own implementation of a my_strtok () function
#include <string.h>
#include <stdio.h>
char * my_strtok(char *str,char *demion);
char *my_strtok(char *str,char *demion)
{
static char *p_last = NULL;
if(str == NULL && (str = p_last) == NULL)
{
return NULL;
}
char *s = str;
char *t= NULL;
while(*s != '\0')
{
t= demion;
while(*t != '\0')
{
if(*s == *t)
{
p_last = s +1;
if( s - str == NULL)
{
str = p_last;
break;
}
*s = '\0';
return str;
}
t++;
}
s++;
}
return NULL;
}
int main(void)
{
char str[] = "liusen,lin,lll";
char * result = NULL;
result = my_strtok(str,",");
printf("%s\n",result);
result = my_strtok(NULL,",");
printf("%s\n",result);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
Operating results:
liusen
the LIN
Press the any Key to the Continue