Search code examples
c++isalpha

What is time complexity of "isalnum(char)" in c++?


isalnum(char) is a method which tells us, whether a given character is alphanumeric or not. What is the time complexity of this small function?

I have written a small subroutine :

bool check(string s,int i)
     {
        if((s[i]>='a' && s[i]<='z') ||
           (s[i]>='0' && s[i]<='9') ||
          (s[i]>='A' && s[i]<='Z'))
        {
            return true;
        }     
        return false;
    }

Is the above subroutine works same as isalnum(char) method? Are there time complexities same?


Solution

  • These functions are different, because isalnum takes int. If you ask about the actions they perform, they are also different.

    isalnum('\xdf'), default C locale, returns false
    isalnum('\xdf'), ISO-8859-1 locale, returns true
    check("\xdf", 0) always returns false.
    

    Time complexities in the both cases are similar, O(1). Your function may be faster, since it does not use locales.