Click here to Skip to main content
15,903,839 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
To count the number of each even digit and total digit in this string "127a33069b1708r831c8457". Don't use any libraries. For example, the output result is as
2-1 time
0 -- 2 times
6 -- 1 time
8 -- 3 times
4 -- 1 time
Total digit - 19

What I have tried:

2-1 time
0 -- 2 times
6 -- 1 time
8 -- 3 times
4 -- 1 time
Total digit - 19
Posted
Updated 2-Jan-23 22:22pm
Comments
Graeme_Grant 3-Jan-23 1:25am    
Walk the line of characters and count as you go ...
[no name] 24-Apr-23 3:28am    
looks like what you "got" not what you "tried", how did you get it?

Start with a program that counts the occurrences of the character '0' in the string (a simple loop and one variable are just enough...).
Then, generalize, possibly using an array of counters.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
There are many ways that you could solve this, ranging from the incredibly simple, through to dramatic overkill. The thing that they all have in common is that they require you to break things down, step by step.

Let's analyse the problem here. You are starting with a fixed string (127a33069b1708r831c8457), and you want to count the occurrence of each digit. From this, we know that we are limiting ourselves to numbers.

A simple pseudo-code solution to this could be:
Create a data structure to hold the character, and the count of times you have seen the character
Loop over the string until we have read all the characters
  Get the character at the current index in the loop
  If the character is a number
    Look for the character and see if it is present in the data structure
    If it is present, increment the count End If
    If it is not present, add it and set the count to 1 End If
  End If
End Loop
Create a counter to hold the total number of matched digits
Loop over the data structure
  Print out the character and count
  Add the count to the total in the counter above
End Loop
Print out the counter
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900