strsep function usage?
时间: 2023-09-04 22:06:51 浏览: 111
C语言分隔字符串函数接口strsep
5星 · 资源好评率100%
The `strsep` function is used to extract tokens from a string. Here's how you can use it:
```c
char str[] = "hello world,how are you";
char *token;
while ((token = strsep(&str, ",")) != NULL) {
printf("%s\n", token);
}
```
In this example, `strsep` is used to split the string `str` into tokens separated by commas. The function takes two arguments: a pointer to a string pointer (`&str`), and a delimiter character (`,` in this case).
The function modifies the original string pointer by replacing the delimiter with a null character (`\0`), and returns a pointer to the start of the extracted token. This process continues until the entire string has been traversed, and the function returns a null pointer.
In the above example, the `while` loop continues to extract tokens from `str` until there are no more tokens left. Each extracted token is printed out using `printf`.
阅读全文