Counting number of digits in a number

Hi everyone. So for my c++ class I have to write a program that converts a decimal number and turns it to a ternary number in base 3. So I managed to do that. Then the questions asks to show how many zeros, ones, and twos are in the ternary number since we are only using base 3. So for example if I have the ternary number: 110122, I would have output this:
Number of Zeros: 1
Number of Ones: 3
Number of Twos: 2

There is supposed to be 3 variables that store the number of 0's, 1's, and 2's. I'm not sure what to use or how to start it. Keep in mind this is a beginners c++ class so nothing really advanced. Thanks
So for my c++ class I have to write a program that converts a decimal number and turns it to a ternary number in base 3.


So, you need to declare a variable, "int number;";

Then the questions asks to show how many zeros, ones, and twos are in the ternary number since we are only using base 3.


You can convert the number to a std::string. Then you iterate on each character element of the std::string, count all 0(s) and 1(s) and 2(s) you can find.

Writing some pseudo-code would be also helpful I guess.
Hope this helps.
Topic archived. No new replies allowed.