Counting occurences in C

Reply

Join Date: Oct 2005
Posts: 6
Reputation: stabule is an unknown quantity at this point 
Solved Threads: 0
stabule stabule is offline Offline
Newbie Poster

Counting occurences in C

 
0
  #1
Oct 15th, 2005
Hi all.
I need some help counting specific numbers in C. The programme should accept two sets of numbers. It should then count how many of the second number is in the first set.

For example:
First input = 23387
Second input = 3
Ouput = "Number 3 appears twice in 23387."

Can someone help me out with the coding ? I'd really appreciate any help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,561
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1611
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting occurences in C

 
0
  #2
Oct 15th, 2005
The easiest way is to input both as strings, not integers, then simply iterator through the string. Attempting to use integers makes that a lot more difficult.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: stabule is an unknown quantity at this point 
Solved Threads: 0
stabule stabule is offline Offline
Newbie Poster

Re: Counting occurences in C

 
0
  #3
Oct 15th, 2005
Mind giving me a headstart ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,561
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1611
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting occurences in C

 
0
  #4
Oct 15th, 2005
Here is one way to do it. There are probably several other ways too.
  1. std::string n = "1233321";
  2. char s = '3';
  3. int count = 0;
  4. for(int i = 0; i < n.length(); i++)
  5. if(n[i] == s)
  6. count++;
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: stabule is an unknown quantity at this point 
Solved Threads: 0
stabule stabule is offline Offline
Newbie Poster

Re: Counting occurences in C

 
0
  #5
Oct 15th, 2005
Thanx bro. I'll try it out. Cheers.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1479 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC