![]() |
Counting occurrence of numbers in C
Hi all.
I need help creating a prog. in C that can count the occurrence of numbers. The prog. should go like this: Enter input 1 = 223335 Enter input 2 = 2 Output = The number 2 appears twice in "223335". The first input can be any 6 numbers and the second input should only be 1 number. Could someone help me out? |
Investigate the modulus and division operators. Also, a trip to forum's rules/FAQ would be in order, and a "How to Post...." thread. There's plenty of help available here if you don't denigrate our rules and preferences.
|
The easiest way to do this is have the first number inputted as a string, and the second one as a character, and just search through the string with a loop
|
DaWei - that's okay - this was posted on every C forum out there... with about the same lack of success from a homework poster's perspective - no completely working code.
Sometimes I wish they's stop teaching C out there so we'd get something approximating actual questions. Instead of homework. |
here it is in C++... surely you can convert it to C...
:
#include <iostream> |
The reason I suggest division and the modulus operator is because your post specifies "numbers". If you use a string, then you'll have to validate it first, which somewhat tosses the "easiest" description. If you acquire the information using scanf, you'll get validation or failure as part of the package. Counting the occurrence of a digit with division/modulus is every bit as simple as counting with a pointer.
:
#include <stdio.h> |
I like DaWei's approach moreso than my own... mine lacks error handling and validation for sure.
|
| All times are GMT -5. The time now is 2:10 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC