C++ equivalent to the Python len() function? -


i have integer , need find out how many digits in it.

a little tricky handle negative numbers , case input zero:

int length(int n) {    int len = 0;    if (n < 0) { len = 1; n = -n; }    while (n > 9) {         n /= 10;         len++;    }    return len+1; } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -