The strcspn function scans the characters in the string, stops when it encounters a character found in charset, and returns the length of the string's initial segment formed by characters not found in charset. If none of the characters match in the character strings pointed to by str and charset, strcspn returns the length of string.