strspn, strcspn — search a string for a set of characters
#include <string.h>
size_t strspn( | s, | |
accept) ; |
const char * | s; |
const char * | accept; |
size_t strcspn( | s, | |
reject) ; |
const char * | s; |
const char * | reject; |
Funkce nám řeknou, kolik znaku na počátku řetězce je (strspn()
) a nebo není (strcspn()
) z uvedené množiny znaků.
Funkce mohou být použity například pro přeskočení bílých mezer.
#include <stdio.h> #include <string.h> int main() { int to_skip; char *line = " \tvalue, value"; to_skip = strspn(line, " \t\n"); printf("%s\n", line+to_skip); return 0; }
Nebo pro nalezení konce identifikátoru.
#include <stdio.h> #include <string.h> #define ID_CHARS "abcdefghijklmnopqrstuvwxyz0123456789" #define DELIM_CHARS " =," int main() { char *line = "ab34c=\tvalue, value", *s=line; int to_eat; to_eat = strcspn(line, "="); printf("%s\n", line+to_eat); return 0; }