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

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

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