#include #include #include // npos: sentinel value returned by the search functions when no match // is found (mirrors std::string::npos, but as a signed int, -1). const int npos = -1; // mystrlen(p) // Returns the number of characters in p, not counting the '\0' terminator // (equivalent to strlen in , which we are not allowed to use). int mystrlen(const char *p){ } // find(p, n, c) // Searches for the first occurrence of character c in the range [p, p + n). // Returns a pointer to the location of c if found, or NULL if c is not in the specified range. // If n = 0, the range is empty and the function must return NULL. const char * find(const char *p, int n, char c) { } // find_first_of(p, pos, q, count) // Searches for the first character in the range [p + pos, p + strlen(p)) that is also in the range [q, q + count). // Returns the position of the first matching character in p if found, or -1 if no match is found. // If pos >= strlen(p), the search range is empty and the function returns -1. int find_first_of(const char *p, int pos, const char *q, int count) { } // find_first_not_of(p, pos, q, count) // Searches for the first character in [p + pos, p + strlen(p)) that is not in [q, q + count). // Returns the position of the first character in p that does not match any character in q, or -1 if all characters match. // If pos >= strlen(p), the search range is empty and the function returns -1. int find_first_not_of(const char *p, int pos, const char *q, int count) { } // find_last_of(p, pos, q, count) // Searches for the last character in [p, p + pos] that matches any character in [q, q + count). // Returns the position of the last matching character in p if found, or -1 if no match is found. // If pos >= strlen(p), pos is clamped to strlen(p) - 1 (i.e., the last valid index). // If p is empty, the function returns -1 int find_last_of(const char *p, int pos, const char *q, int count) { } // find_last_not_of(p, pos, q, count) // Searches for the last character in [p, p + pos] that does not match any character in [q, q + count). // Returns the position of the last character in p that does not match any character in q, or -1 if all characters match. // If pos >= strlen(p), pos is clamped to strlen(p) - 1 (i.e., the last valid index). // If count = 0, no characters can match, so every character in p is considered "not of" q. The function returns the index of the last character in [p, p + pos]. // If p is empty, the function returns -1 int find_last_not_of(const char *p, int pos, const char *q, int count){ } int main(void) { /////////////////// find //////////////////////////// const char *p = "hello world"; assert(find(p, 1, 'l') == NULL); assert(find(p, 1, 'h') == p); /////////////////// find_first_of /////////////////// assert(1==find_first_of("alignas", 0, "klmn", 4)); assert(-1==find_first_of("alignof", 0, "wxyz", 4)); /////////////////// find_first_not_of /////////////////// assert(find_first_not_of("hello world", 0, "helo wrd", mystrlen("helo wrd")) == -1); assert(find_first_not_of("hello world", 0, "helo wr", mystrlen("helo wr")) == 10); /////////////////// find_last_of //////////////////// assert(5==find_last_of("alignas", 6, "abcd", 4)); assert(find_last_of("hello", 4, "", 0) == -1); /////////////////// find_last_not_of //////////////////// assert(find_last_not_of("hello world", mystrlen("hello world") - 1, "helo wrd", mystrlen("helo wrd")) == -1); assert(find_last_not_of("hello world", mystrlen("hello world") - 1, "helo wr", mystrlen("helo wr")) == 10); }