0

This program asks us to enter a car license number(6 digits). Then count how much of every character there is. Display it

So my programming teacher tasked us to do something like this on our programming test the other day and it gave quite some trouble( programming noob) Can someone like show me the code to make this possible? Thanks ***In c++*** just to be clear haha :)

6th Mar 2017, 6:34 PM
Adiel Cowo
Adiel Cowo - avatar
2 Answers
+ 1
If you are not required to check the correctness of the data, and you know you are dealing with numbers, you can use an array of 10 places, 1 for each digit from 0 to 9. When you go over the license plate number, you add 1 to each index in the array that corresponds to that digit. What I did below is divide the number that represents the license plate by 10 each time, factoring the remainder to give me the right most digits. That way I am separating the license plate into it's digits. int licensePlate; int digits[10]; int rightMostDigit; while(licensePlate > 0) { rightMostDigit = licensePlate % 10; digits[rightMostDigit]++; licensePlate = licensePlate / 10; } return digits; Another way, could be to convert the licensePlate to string and go over it in a similar fashion. Hope that helps.
6th Mar 2017, 7:44 PM
Tomer
0
Thank you!
6th Mar 2017, 11:19 PM
Adiel Cowo
Adiel Cowo - avatar