Efficient way to find length of integer in c++? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 4

Efficient way to find length of integer in c++?

Looking for an easy and efficient meathod to find length of an integer. The integer can be negative or positive. I cannot include any libraries.

28th Jun 2017, 2:16 AM
Jordan Chapman
Jordan Chapman - avatar
5 Respostas
+ 9
This is an algorithm which I just came up with. Please test and see if it works for all values. https://code.sololearn.com/cv0xQoe9xit7/?ref=app
28th Jun 2017, 5:05 AM
Hatsy Rei
Hatsy Rei - avatar
28th Jun 2017, 3:40 AM
ź¹€ģ •ģ œ(Legacy)
ź¹€ģ •ģ œ(Legacy) - avatar
+ 2
Divide by 10 until the answer is < 1? I haven't completely thought this through... edit: yeah...negatives. Glad others popped in :)
28th Jun 2017, 2:57 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
@Little Grass Though "-" is easily fixed, unfortunately to_string() is not in gcc (and it requires a lib).
28th Jun 2017, 3:35 PM
Kirk Schafer
Kirk Schafer - avatar
- 2
to_string(a).size()
28th Jun 2017, 3:19 PM
Little Grass