char * strtok ( char * string, const char * delimiters );
Sequentially truncate string if delimiter is found.
If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function
overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found
a delimiter.
delimiters may vary from a call to another.
Parameters.
- string
- Null-terminated string to scan. separator
- Null-terminated string containing the separators.
Return Value.
A pointer to the last token found in string. NULL is returned when there are no more tokens to be found.
Portability.
Defined in ANSI-C.
Example.
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This is a sample string,just testing.";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
return 0;
}
Output:
Splitting string "This is a sample string,just testing." in tokens:
This
is
a
sample
string
just
testing
本文详细介绍了C语言中的字符串处理函数strtok的用法。strtok用于将字符串分割成多个子串(标记),并能处理多种分隔符。文章通过一个示例程序展示了如何使用strtok来按指定的分隔符分割字符串。
1457

被折叠的 条评论
为什么被折叠?



