Previous Page
Next Page


Searches for any element of one string in another

#include <string.h>
int strcspn ( 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:


See Also

strspn( ), strpbrk( ), strchr( ), wcscspn( )

Previous Page
Next Page