When I scour the C for functions to assist my code, if often overlook a few that seem dull or confusing. One of those I’ve always glossed over is the strspn() function, as well as its counterpart, strcspn().
These functions scan a string for the first occurrence of a given character — or for not of a given character.
For example, if you want to discover where the first comma in a string is located, you use this statement:
r = strcspn("Hello, world!",",");
The return value r
contains 5, which is the offset (starting with zero) where the comma is found in the string (char array).
Here are the two functions’ prototypes:
size_t strspn(const char *s, const char *charset);
size_t strcspn(const char *s, const char *charset);
The first argument, s
, is the string to search. The second argument, charset
, is a string containing search characters. The return value is the offset within s
where one of the characters in charset
is either not found for strspn() or not found for strcspn().
Both functions are prototyped in the string.h
header file.
The strspn() function keeps looking through the first string as long as characters in the second string are found. Once a character in the first string doesn’t exist in the second, its offset is returned. The following code demonstrates:
2021_11_27-Lesson-a.c
#include <stdio.h> #include <string.h> int main() { char a[] = "HELLO, WORLD!"; char b[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; size_t r; r = strspn(a,b); printf("%zu = '%c'\n",r,a[r]); return(0); }
At Line 10, the strspn() function scans string a[]
. As long as it finds characters in string b[]
(the uppercase alphabet), it’s happy. When a non-matching character is encountered, its offset (starting at zero) is returned in variable r
. This value is output, along with the offending character:
5 = ','
The strcspn() function does the opposite of strspn(): It scans the first string until it finds a character available in the second:
2021_11_27-Lesson-b.c
#include <stdio.h> #include <string.h> int main() { char a[] = "Hello, world!"; char b[] = " ,!"; size_t r; r = strcspn(a,b); printf("%zu = '%c'\n",r,a[r]); return(0); }
Above, string b[]
contains the non-alphabetic characters from string a[]
. The strcspn() function at Line 10 searchings string a[]
for the first occurrence of any of these characters. Here’s the output:
5 = ','
The output is the same as the first example, though in this case the purpose of string b[]
is changed: It contains characters that stop the scan. Array element a[5]
is the comma, which stops the scan after the “o” in “Hello.”
I can think of a few practical uses for both the strspn() and strcspn() function, though I’m more attracted to the strcspn() function. In next week’s Lesson I explore one way to use this function to plow through a string and pluck out words.