-
-
Save smarthall/1935568 to your computer and use it in GitHub Desktop.
strnchr because it doesn't exist for some reason
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
/* | |
Returns a pointer to the first occurrence of character in the C string str. | |
The terminating null-character is considered part of the C string. Therefore, | |
it can also be located to retrieve a pointer to the end of a string. | |
@param str the string to be searched | |
@param len the number of characters to search | |
@param character the character to search for | |
@returns A pointer to the first occurrence of character in str. | |
If the value is not found, the function returns a null pointer. | |
*/ | |
const char *strnchr(const char *str, size_t len, int character) { | |
const char *end = str + len; | |
char c = (char)character; | |
do { | |
if (*str == c) { | |
return str; | |
} | |
} while (++str < end); | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment