# String/Int Length Issue

• 07-25-2010
xSmiley
String/Int Length Issue
Hello, I have just recently (yesterday) began learning cpp. I have been going over the tutorials offered on this site and I have stumbled across a small problem. I am attempting to create a tester program to see if I am grasping what I have learned so far. My problem occurs when I attempt to discover the length of an integer.

I am aware of the strlen function that will return the length of a string. However I desire to know the length of variable that is an integer.

So far I have tried making a string that variable but the compiler refuses on the grounds that it cannot accept a 'char string' is equal to an 'int variable'. Like so
Code:

```... char vse[15]; int vx; ... vse=vx; ...```
I understand that it cannot be done this way.

Then I reread typcasting, in hopes that I could find a way to play with the int/char types and be allowed to convert my int variable into a string and use strlen. Unfortuneatly I cannot figure out a feasible way to use typcasting to solve my problem.

In short, I am looking for a way to know how many digits a number has.
ie:
1 -->1
11 -->2
111 --> 3
1111 --> 4
...

Is there a way to find this?

Cheers,
Smiley
• 07-25-2010
anon
You could try logarithms, or you could just count how many times you can divide an integer by ten.
• 07-26-2010
iMalc
You use stringstreams.
Code:

```stringstream s; s << 1408; // or whatever number or variable```
s.str() now holds the std::string for you, which is what you should be using for text.
To quote Zahlman (from another forum):
Quote:

As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.
• 07-26-2010
xSmiley
Ah of course! Thank you Anon, I can't beleive I did not think to count the number of times I could divide the number by 10. I created this litte function that has acheived my goal flawlessly, thank's again Anon.

Code:

```int digam(int oi) {   if (oi!=0) {     int in = 0;     while (oi>0) {       in++;       oi /= 10;     }     return in;   }   else {     return 1;     } }```
I though this the most logical approach to doing this. Although I suspect it might not be as optimised as it could be, it works nonetheless.

iMalc, you also bring an answer to my question, and a very interesting one. I wonder, though, is 'stringstream' a variable type(like 'int' and 'char')? Also, when you call it s.str() is that what I should type if I want to return the value/word?

I'm sorry for having several questions but this seems like a very interesting and useful method to learn, and I would much like to understand.

Again I thank the both of you, you're help was greatly appreciated.

Cheers,
Smiley
• 07-26-2010
iMalc
Yes stringstream is a type and so is string. They live in the std namespace.
Here is a complete exmple that does the reverse, it converts numeric strings back into integers:
stringstream - C++ Reference
Unforunately I don't have time to give better examples right now.
• 07-26-2010
CornedBee
The do...while loop allows you to write the function a bit more concisely:
Code:

```unsigned digits(unsigned n, unsigned radix = 10) {   unsigned d = 0;   do {     ++d;     n /= radix;   } while(n > 0);   return d; }```
• 07-27-2010
iMalc
Yeah it irks me too when someone uses a while loop for that. Even a 0 is one digit long so clearly a do .. while is appropriate.
• 07-27-2010
xSmiley
Oh, thanks a lot iMalc, knowing this will help a great deal. I have read through the link you gave me a couple of times and it seems to be sinking in. I'll go over it a couple more times and play around with it to be fully comfortable with it. Thanks again.

Cornedbee, thank you for a more concise version of the function. If I understand correctly, the do...while loop is more efficient than a while loop? Is this because the do..while executes the loop once and then checks to go on, instead of checking every-time before executing, as the while loop does?

I apologies if this all sounds overly basic, but I like to be sure of my facts when I learn something. If the foundation is weak then the building cannot hope to be solid.

Thanks again,
Smiley
• 07-27-2010
CornedBee
Quote:

Originally Posted by xSmiley
Cornedbee, thank you for a more concise version of the function. If I understand correctly, the do...while loop is more efficient than a while loop? Is this because the do..while executes the loop once and then checks to go on, instead of checking every-time before executing, as the while loop does?

It might be more efficient in this particular case, but what's important is that the code has lower complexity. It's easier to read (if you know how do...while works, at least). Efficiency on this low level is irrelevant; the compiler will know what to do.
• 07-27-2010
xSmiley
Ah, I admit it does allow for a better read. Thanks again for the help, I honestly appreciate your time.

Cheers,
Smiley