tags:

views:

118

answers:

4

Hello - I'm trying to write a function that'll detect if a digit is found in a number:

// returns 1 if source contains num, 0 otherwise
int contains_num(source, num);

for example, contains_num(12345, 3) returns 1 and contains_num(12345, 6) returns 0.

I'm not sure how to go about solving this. Probably can't use pointers, arrays and such.

Any ideas? thanks.

A: 

This should work:

int Contains_Num(int source, int num)
{
  if (source == 0 && num == 0) return 1;

  int tmpSource = source;
  while (tmpSource != 0)
  {
    if (tmpSource % 10 == num) return 1;
    tmpSource /= 10;
  }
  return 0;
}
Sani Huttunen
In case `num` is negative, the condition should be `while (tmpNum)`.
wallyk
Why did you simply answer someone else's homework question? The point of homework is to think and learn, not to ask on a forum and be given the answer.
Brian Campbell
Good point. Fixed.
Sani Huttunen
Doesn't work for zero. And why do you think you need tmpNum?
anon
@Brian Campbell: Because it's Christmas Eve...
Sani Huttunen
@Brian: One thing I've puzzled over with Stack Overflow is why folks are so adverse to giving away free answers to homework but are okay with giving away free answers for someone's paying job. Shouldn't we charge for that advice too?
Otis
-1 for posting code in response to homework question
qrdl
@Otis The thing about a paying job is that we can all work more efficiently if when stuck, we help each other out. I answer someone else's question, and someone answers mine, and it increases everyone's productivity. For homework, however, the point of assigning the question is not to just get it done, it's to help you learn to think in new ways; learn to think logically about a problem, decompose it into smaller problems, and so on. If someone just posts the answer outright, you don't get that practice, and don't learn what the homework was trying to teach.
Brian Campbell
If you take a look at my other answers you'll see I never answer homework questions. Thanks for the downvotes for trying to be generous on Christmas...
Sani Huttunen
Sorry, I didn't mean to spoil your Christmas; I did not downvote you, I just thought I would point out that it's better to try and give people with homework questions a hint rather than a fully-formed solution. I understand that you were trying to be generous and helpful, and I appreciate that you tried to provide a prompt, correct answer to the question, but with homework it's more helpful to help guide someone to their own solution.
Brian Campbell
+3  A: 

Since this is homework, I don't want to give you the entire answer right away. Instead, I'll ask a series of questions, and let you answer in the comments, so I'll simply be guiding you to the correct answer rather than giving it to you outright.

So, you have a number, and you want to break it down into individual digits. Can you think of a way of extracting one digit from the number, say, the last one? Think about what each digit represents, and how you might be able to isolate one digit from the rest of them.

Brian Campbell
good call, by dividing by 10 I get the LSB each time, which I could then check against the number. Didn't think about that - thanks!
sa125
A: 

Your answer is dependent on the base a number is represented in. For example, the number 255 contains 5 when written in base 10, but in base 16, it does not. Your base seems to be 10.

So what you want to do is to look at the last digit of a number, and see if it is the digit you want. The last digit can be easily found using the modulo operator (%). If it is the digit you want, you're done. If not, and if there are more digits, you can discard the last digit and repeat the process again for the number obtained by dividing the original number by 10 and discarding the fractional part. In C, the division operator, /, does this for you automatically if both the operands of it are of integer type.

When you run out of digits because division gives you 0, you are sure that the number doesn't contain the digit you wanted.

Alok
yeah, we're dealing with base 10. tackling other bases has a similar approach, but I was spared that for the time being :)
sa125
A: 

An alternative way of looking at the problem is:

  • Does the string representation of the number contain a specific digit?

It is relatively easy to convert the number to a string; there are string searching functions to find whether a specific character appears in the string.

And this has the merit of working on negative numbers whereas some of the suggestions I've seen fail on negative numbers (and most of the rest do not address the issue explicitly). (Question for you: why?)

Jonathan Leffler