# Thread: Counting number of digits?

1. ## Counting number of digits?

Code:
```int dat;

printf( "\nEnter an int: " );
scanf( "%d", &dat );

printf( "\nThe counts of digits:\n0  %d\n"
"1  %d\n"
"2  %d\n"
"3  %d\n"
"4  %d\n"
"5  %d\n"
"6  %d\n"
"7  %d\n"
"8  %d\n"
"9  %d\n", );```
I'm trying to get the user to enter an integer and then make the output print the number of digits entered. Ex. If I enter 1123 or some other number, how do I make it display this? ( and for any number I enter )
0 0
1 2
2 1
3 1
4 0
5 0
6 0
7 0
8 0
9 0

The right column shows the numbers I entered and how many times it occured. I'm not sure know how to extract the digit using loops, "if", etc and compare it with the # to make it show the number of times it occured.

2. For starters, how would you isolate the individual digits of the integer entered by the user?

Another thing to think of: must you actually store the input as an int, or can you just store it as a string?

3. I think I would enter dat % 10 to get the 1st digit. (dat * .1) % 10 to get the 2nd digit and so on. Then do I use "if" to see what digit it is? After I get the digit, how would I put it back in the printf function?

4. Originally Posted by scatterice
I think I would enter dat % 10 to get the 1st digit. (dat * .1) % 10 to get the 2nd digit and so on.
Remember that dat * .1 is the same as dat/10, which is easier to read and satisfies the constraints of integer division. Now, get a new variable (or just stick with the old one), assign it the value of dat/10 and repeat the whole thing until dat/10 < 10.

Greets,
Philip

5. I see a printf() call containing 10 %d conversions and NO parameters.
What are you expecting here?

6. it limited for int (when you are enter a big value it will transform to anything)

output:

Code:
```[guest@station eng]\$ ./test
Enter a number: 1237034079
0 - 2
1 - 1
2 - 1
3 - 2
4 - 1
5 - 0
6 - 0
7 - 2
8 - 0
9 - 1
[guest@station eng]\$```

7. Hello.

As laserlight already suggested, I would solve this problem by using input as a string and not as int. This method has several advantages over the integer method:
1. it's not limited by the maximum number the int can hold
2. it's easier to code
3. it teaches you about character encoding, which is something really useful

AS you probably know, character are (like everything else) represented by numbers. Mapping of characters to numbers is called encoding and it enables us to convert numbers to something we're able to read.

There are many encodings used in computing, but most prevalent are ASCII, ISO-8859-1 and UTF-8. And really important thing to know is that all of them map first 128 character exactly the same way. You can see ASCII table here.

Now to solve the problem. Steps that are involved, could be described as:
1. obtain input from user
2. parse and validate input
3. return results
Obtaining input from user should be done like this, since we need a string:
Code:
`scanf( "%s", string );`
Now that we have data to work on, we can examine characters in the string on by one and validate the input. If you looked at the ASCII charmap I provided link to, you can see that digits are mapped to numbers 48 to 57 and this is what we're after. We can test this using simple if statement:
Code:
`if( character > 47 && character < 58 )`
or
Code:
`if( character >= '0' && character <= '9' )`

8. Just use some basic math in a loop. This is a common homework problem. That's the common solution.

Quzah.