* Find a substring within a string. * * @param h The haystack string. * @param n The needle string (substring). * @return A pointer to the first occurrence of the substring in * the string, or NULL if the substring was not encountered within the string. */
| 265 | * the string, or NULL if the substring was not encountered within the string. |
| 266 | */ |
| 267 | char *strstr(const char *h, const char *n) |
| 268 | { |
| 269 | size_t hn = strlen(h); |
| 270 | size_t nn = strlen(n); |
| 271 | |
| 272 | if (hn < nn) |
| 273 | return NULL; |
| 274 | |
| 275 | for (size_t i = 0; i <= hn - nn; i++) |
| 276 | if (!memcmp(&h[i], n, nn)) |
| 277 | return (char *)&h[i]; |
| 278 | |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Separate strings. |