Search code examples
cstrchr

First index of a non-Whitespace character in astring in C


How do I get the index of the first non-whitespace character in a string. For example for the string " #$%abcd" I would expect getting the index 3 for #.


Solution

  • Why not code it instead of include other libraries:
    Here is a starting point:

    int main () 
    {
        char s[] = "   #$%abcd\0";
        size_t i = 0;
        while(s[i] == ' ' || s[i] == '\t'|| s[i] == '\n' || s[i] == '\r' || s[i] == '\f' || s[i] == '\v')
        {
            ++i;
        }
        return i;
    }
    

    i is the index of the first non-whitespace char: