Name
strcspn
Synopsis
Searches for any element of one string in another
#include <string.h> intstrcspn
( const char *s1
, const char *s2
);
The strcspn()
function
returns the number of characters at the beginning of the string
addressed by s1
that do not match any of
the characters in the string addressed by
s2
. In other words, strcspn()
returns the index of the first
character in s1
that matches any
character in s2
. If the two strings have
no characters in common, then the return value is the length of the
string addressed by s1
.
Example
char *path = "/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games";
int separator;
char *basename = "aprogram";
char fullname[1024] = "";
separator =strcspn
( path, ":" ); // Obtain the index of the first colon.
strncpy( fullname, path, separator );
fullname[separator] = '\0'; // Terminate the copied string fragment.
strncat( fullname, "/", sizeof(fullname) − strlen(fullname) −1 );
strncat( fullname, basename, sizeof(fullname) − strlen(fullname) −1 );
puts( fullname );
The last statement prints the following string:
/usr/local/bin/aprogram
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.