3

In Java, I use

int length = String.valueOf(input).length();

to find the length of an integer.

My question is: Are there any similar ways to do so in C++? I have already tried the for loops and while loops such as:

while (input > 0){
    input/=10;
    count++;

So, apart from the loops are there anything else available in C++. Thank you for your answer.

Raphael
  • 161
  • 1
  • 2
  • 8
  • http://en.cppreference.com/w/cpp/string/basic_string/to_string – n. m. could be an AI Nov 03 '15 at 06:15
  • @CrakC - I already said no loops. – Raphael Nov 03 '15 at 06:17
  • @n.m. - Can you please cite an example? If possibe. – Raphael Nov 03 '15 at 06:17
  • 1
    @Raphael There are other solutions on the page as well, including a similar version of the one posted below. Did you go through all of that before posting a new question? – Swastik Padhi Nov 03 '15 at 06:18
  • @CrakC - Sorry, I just saw the accepted answer. – Raphael Nov 03 '15 at 06:19
  • @Raphael Never mind. Please do a thorough search before posting a new question. If you get a solution from there then please remove this question. – Swastik Padhi Nov 03 '15 at 06:20
  • @Raphael : I see you are new on StackOverflow. If you feel that one of the answers has adequately satisfied your question, you are encouraged to accept it by clicking on the green tick next to the answer. This helps mark the question as solved with accepted solution, and allows users to focus on other questions. Thanks :) – therainmaker Nov 11 '15 at 04:39

5 Answers5

10

If you want an exact counterpart of what you have written in Java, you can use:

int length = to_string(input).length();

Note that to_string is a C++11 feature. Also, be careful with negative numbers.

therainmaker
  • 4,253
  • 1
  • 22
  • 41
3

The number of digits can be calculated without converting to a string first by using the number's logarithm:

std::size_t intlen(int i) {
    if (i == 0) return 1;
    else if (i < 0) return 2 + static_cast<std::size_t>(std::log10(-i));
    else if (i > 0) return 1 + static_cast<std::size_t>(std::log10(i));
}

The logartihm is only defined for positive numbers, so negatives and zero have to be handled separately, counting the - sign as an additional character. Replace log10 by log2 to obtain the number of binary digits (this is possible for any base).

Note however that converting to strings first (e.g. by using std::to_string) is a locale-dependent operation and can thus yield different results for different language settings - some locales insert a thousands separator (e.g. 100,000) which will not show up using the above formula.

Fabian Knorr
  • 3,134
  • 3
  • 20
  • 32
2

unsigned int number_of_digits = 0;

do { ++number_of_digits; n /= base; } while (n);

// n is your base number.

maxadorable
  • 1,290
  • 1
  • 10
  • 24
2

Talking about pre-C++11, you can use the same approach, but with sprintf.

Convert integer to a char array, and then get its length:

char buffer[30];

int length = sprintf(buffer, "%d", input);

Here is the working IDEOne example.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
2

Apart from the loops there is recursion. For example, for positive integers you can do:

unsigned int len(unsigned int n)
{
    return n ? len(n/10)+1 : 0;
}
Paul
  • 13,042
  • 3
  • 41
  • 59