December 2005
Beginner to intermediate
618 pages
20h 19m
English
strcspn
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.
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
Read now
Unlock full access