Defined in header
<string.h>
| ||
char *strtok( char *str, const char *delim );
| (until C99) | |
char *strtok( char *restrict str, const char *restrict delim );
| (since C99) | |
Finds the next token in a null-terminated byte string pointed to by str
. The separator characters are identified by null-terminated byte string pointed to by delim
.
This function is designed to be called multiples times to obtain successive tokens from the same string.
- If str != NULL, the call is treated as the first call to
strtok
for this particular string. The function searches for the first character which is not contained indelim
.
-
- If no such character was found, there are no tokens in
str
at all, and the function returns a null pointer. - If such character was found, it is the beginning of the token. The function then searches from that point on for the first character that is contained in
delim
.
-
- If no such character was found,
str
has only one token, and future calls tostrtok
will return a null pointer - If such character was found, it is replaced by the null character '\0' and the pointer to the following character is stored in a static location for subsequent invocations.
- If no such character was found,
- The function then returns the pointer to the beginning of the token
- If no such character was found, there are no tokens in
- If str == NULL, the call is treated as a subsequent calls to
strtok
: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as str.
Parameters
str | - | pointer to the null-terminated byte string to tokenize |
delim | - | pointer to the null-terminated byte string identifying delimiters |
Return value
Pointer to the beginning of the next token or NULL if there are no more tokens.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *str = (char*)malloc(20);
char *tok = NULL;
int len = 0;
strcpy(str, "This is a string");
len = strlen(str);
printf("string before strtok(): %s\n", str);
tok = strtok(str, " ");
while (tok) {
printf("Token: %s\n", tok);
tok = strtok(NULL, " ");
}
printf("Print mangled string after strtok()\n");
for (int i = 0; i < len; i++) {
if (str[i] == '\0') {
printf("'\\0'");
} else {
printf("%c", str[i]);
}
}
printf("\n");
free(str);
return 0;
}
输出为
string before strtok(): This is a string
Token: This
Token: is
Token: a
Token: string
Print mangled string after strtok()
This'\0'is'\0'a'\0'string
Defined in header
<string.h>
| ||
size_t strspn( const char *dest, const char *src );
| ||
Returns the length of the maximum initial segment of the byte string pointed to by dest
, that consists of only the characters found in byte string pointed to by src
.
Parameters
dest | - | pointer to the null-terminated byte string to be analyzed |
src | - | pointer to the null-terminated byte string that contains the characters to search for |
Return value
The length of the maximum initial segment that contains only characters from byte string pointed to by src
strspn返回dest中包含src连续段最长的个数
#include <string.h>
#include <stdio.h>
int main(void)
{
char *string_find = "abcde312$#@";
char *low_alpha = "qwertyuiopasdfghjklzxcvbnm";
printf("%zu", strspn(string_find, low_alpha));
return 0;
}
输出为
5
Defined in header
<string.h>
| ||
char* strpbrk( const char* dest, const char* breakset );
| ||
Scans the null-terminated byte string pointed to by dest
for any character from the null-terminated byte string pointed to by breakset
, and returns a pointer to that character.
Parameters
dest | - | pointer to the null-terminated byte string to be analyzed |
breakset | - | pointer to the null-terminated byte string that contains the characters to search for |
Return value
Pointer to the first character in dest
, that is also in breakset
, or null pointer if no such character exists.
#include <stdio.h>
#include <string.h>
int main(void)
{
const char* str = "hello world, friend of mine!";
const char* sep = " ,!";
unsigned int cnt = 0;
do {
str = strpbrk(str, sep); // find separator
if(str) str += strspn(str, sep); // skip separator
++cnt; // increment word count
} while(str && *str);
printf("There are %d words\n", cnt);
}
输出为
There are 5 words
Defined in header
<string.h>
| ||
size_t strcspn( const char *dest, const char *src );
| ||
Returns the length of the maximum initial segment of the byte string pointed to by dest
, that consists of only the characters not found in byte string pointed to by src
.
Parameters
dest | - | pointer to the null-terminated byte string to be analyzed |
src | - | pointer to the null-terminated byte string that contains the characters to search for |
Return value
The length of the maximum initial segment that contains only characters not found in the byte string pointed to by src
Example
#include <string.h> #include <stdio.h> int main(void) { char *string_find = "abcdefg**"; char *character = "*"; printf("%zu", strcspn(string_find, character)); return 0; }
Output:
7