6

How to check if a int var contains a specific number

I cant find a solution for this. For example: i need to check if the int 457 contains the number 5 somewhere.

Thanks for your help ;)

c5754272
  • 61
  • 1
  • 1
  • 2

3 Answers3

15
457 % 10 = 7    *

457 / 10 = 45

 45 % 10 = 5    *

 45 / 10 = 4

  4 % 10 = 4    *

  4 / 10 = 0    done

Get it?

Here's a C implementation of the algorithm that my answer implies. It will find any digit in any integer. It is essentially the exact same as Shakti Singh's answer except that it works for negative integers and stops as soon as the digit is found...

const int NUMBER = 457;         // This can be any integer
const int DIGIT_TO_FIND = 5;    // This can be any digit

int thisNumber = NUMBER >= 0 ? NUMBER : -NUMBER;    // ?: => Conditional Operator
int thisDigit;

while (thisNumber != 0)
{
    thisDigit = thisNumber % 10;    // Always equal to the last digit of thisNumber
    thisNumber = thisNumber / 10;   // Always equal to thisNumber with the last digit
                                    // chopped off, or 0 if thisNumber is less than 10
    if (thisDigit == DIGIT_TO_FIND)
    {
        printf("%d contains digit %d", NUMBER, DIGIT_TO_FIND);
        break;
    }
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91
matt.dolfin
  • 672
  • 4
  • 9
  • 1
    Cool, click the check to mark it as your selected answer if you are satisfied with my help. Happy programming! – matt.dolfin Feb 12 '11 at 18:25
  • hey thanks for the answer but i don't understant why you use modulo 10.is there any rule, relationship for this kind of problem where maybe (just saying) we need to use 5*2? or something? please explain why you use 10 thanks – black sensei Jul 16 '11 at 13:52
  • @black: 10 is used because we are using a base 10 number system. If, e.g., 457 was base 8 instead of base 10, you would use 8 instead. But, if we were using base 8, 8 would be written as 10(base 8) ("one, zero (base eight)" not "ten"), so the above answer would still be correct. Does this make sense? – matt.dolfin Jul 20 '11 at 04:49
  • yes it make perfect sense now.it's like eliminating the digits 1 by 1 from right to left.how about considering only the natural part of the modulo division %10 and find out if their %5 division will be 0 to be sure? thanks – black sensei Jul 20 '11 at 07:51
  • @black : I don't think I'm following what you are suggesting. Consider if the original question stated "check if the int 457 contains the number **7**" or "check if the int **407** contains the number 5. How would you solve these? I would guess the algorithm you are considering wouldn't work for these cases. But like I said, I don't think I follow. – matt.dolfin Jul 20 '11 at 12:43
  • i see my mistake, then i'm not sure i really understand your algorithm either.what will make the different when you have 407 and 457 especially when you get to 40 and 45 ? thanks – black sensei Jul 23 '11 at 13:11
  • @black : Check out the edit to my answer. I should hope this will answer any questions you may still have. If you're still confused, you should probably look up how modulo and integer division operations work. Happy programming! – matt.dolfin Jul 26 '11 at 14:23
4

Convert it to a string and check if the string contains the character '5'.

Yhrn
  • 1,063
  • 7
  • 16
  • which uses an algorithm similar to the modulus loop @matt.dolfin .. so, why to build the string? :) – akira Feb 12 '11 at 10:40
  • It's probably faster for integers with thousands of bits. % is expensive. – Marcus Johansson Feb 12 '11 at 10:42
  • @Marcus Johansson: in order to convert the number to a string you would have to use a similar algorithm as the one @matt.dolfin or @Shakti Singh pointed out. PLUS some overhead to create the string PLUS some overhead to than do a string-search inside the created string. – akira Feb 12 '11 at 11:08
  • just to prove my point that 'convert to string and lookup' is not the best solution for the problem: http://tinodidriksen.com/2010/02/07/cpp-convert-int-to-string-speed/ with the code for converting numbers to string is available http://tinodidriksen.com/uploads/code/cpp/speed-convert-int-to-string.cpp – akira Feb 15 '11 at 08:16
  • @akira: I definitely agree that it is not the most efficient solution. However, depending on where it is used the extra overhead might be insignificant. The advantage is that the code is more readable and less error prone (matter of opinion, of course). I generally go for the 'cleaner' solution first and then optimize if needed. On the other hand, string manipulation can be very expensive if you're not careful and think about how the code will be used. – Yhrn Feb 22 '11 at 13:03
4
int i=457, n=0;

while (i>0)
{
 n=i%10;
 i=i/10;
 if (n == 5)
 {
   printf("5 is there in the number %d",i);
 }
}
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • printf(" .. ") .. double quotes is what c is using for string, not single quotes... and maybe abs(i) would be a good idea. – akira Feb 12 '11 at 10:42
  • in general i would provide a function here with returns 0 on miss and 1 on hit; and just use `while (i != 0)`, that would make it work with negative numbers as well. – akira Feb 12 '11 at 11:09