Skip to content

Advance to the Next Run of a Character

Walk a string to the first occurrence of a character, then to the last character of that run, or to the terminator if it is missing.

Language: C

Added on: 21st August 2026

char *next_char(char *str, char c)
{
    if (str == NULL) {
        return NULL;
    }
    while (*str != '\0' && *str != c) {
        str++;
    }
    if (*str == c) {
        while (*str == c) {
            str++;
        }
        str--;
    }
    return str;
}

Caveats

Returns a pointer into the original string. NULL input returns NULL.