#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, which we are not allowed to use) // requires: p is a valid null-terminated string int mystrlen(const char *p){ int sz = 0; while (*p++) sz++; return sz; } // find(p, n, c) returns a pointer to the first occurrence of c in the range // [p, p + n), or NULL if c is not in that range // requires: [p, p + n) is a valid range, n >= 0 // note: if n = 0, the loop never runs and the function returns NULL const char* find(const char *p, int n, char c) { for (int i = 0; i < n; i++) { if (p[i] == c) return p + i; } return NULL; } // in_set(c, q, count) returns 1 if c occurs in [q, q + count), 0 otherwise // requires: [q, q + count) is a valid range, count >= 0 // note: if count = 0, the set is empty and the function always returns 0 int in_set(char c, const char *q, int count) { return find(q, count, c) != NULL; } // find_first_of(p, pos, q, count) returns the position of the first character // in [p + pos, p + strlen(p)) that is also in [q, q + count), or npos // if there is none // requires: p is a valid string, [q, q + count) is a valid range, pos >= 0, // count >= 0 // note: if pos >= strlen(p) the loop never runs, so npos is returned int find_first_of(const char *p, int pos, const char *q, int count) { int len = mystrlen(p); for (int i = pos; i < len; i++) { if (in_set(p[i], q, count)) return i; } return npos; } // find_first_not_of(p, pos, q, count) returns the position of the first // character in [p + pos, p + strlen(p)) that is not in [q, q + count), or // npos if every character is in it // requires: p is a valid string, [q, q + count) is a valid range, pos >= 0, // count >= 0 // note: if pos >= strlen(p) the loop never runs, so npos is returned int find_first_not_of(const char *p, int pos, const char *q, int count) { int len = mystrlen(p); for (int i = pos; i < len; i++) { if (!in_set(p[i], q, count)) return i; } return npos; } // find_last_of(p, pos, q, count) returns the position of the last character // in [p + pos, p + strlen(p)) that is also in [q, q + count), or npos // if there is none // requires: p is a valid string, [q, q + count) is a valid range, pos >= 0, // count >= 0 // note: pos is clamped to strlen(p) - 1; if p is empty, pos becomes -1, the // loop never runs and npos is returned int find_last_of(const char *p, int pos, const char *q, int count) { int len = mystrlen(p); if (pos >= len) pos = len - 1; for (int i = pos; i >= 0; i--) { if (in_set(p[i], q, count)) return i; } return npos; } // find_last_of(p, pos, q, count) returns the position of the last character // in [p + pos, p + strlen(p)) that is not in [q, q + count), or npos // if every character is in it // requires: p is a valid string, [q, q + count) is a valid range, pos >= 0, // count >= 0 // note: pos is clamped to strlen(p) - 1; if p is empty, pos becomes -1, the // loop never runs and npos is returned int find_last_not_of(const char *p, int pos, const char *q, int count) { int len = mystrlen(p); if (pos >= len) pos = len - 1; for (int i = pos; i >= 0; i--) { if (!in_set(p[i], q, count)) return i; } return npos; }