# Thread: finding an integer in an integer

1. ## finding an integer in an integer

is there any way to search for the occurence of an integer inside an iteger? ie. to try to find '2' in '12' ?

Thanks

2. you could copy that integer to a char and then search.

I dont know of any direct method.

3. You could use itoa to convert the integer to a string and then search for the digit. Ofcourse also the digit must be converted to a string.

4. Thanks for that, Shiro, but would you be able to give me a code example of how to use that function and then search for it? Thanks. I really apprieciate it, mate.

Thanks

5. Code:
```#include <iostream>
#include <sstream>
const char match = '2';

int main ( void )
{
int x = 12, count = 0;
std::ostringstream oss;
oss<<x;
std::string s = oss.str();
for ( int i = 0; i < s.length(); i++ )
if ( s[i] == match ) count++;
std::cout<< count <<" digit(s) match \'"<< match <<"\'\n";
return 0;
}```
or
Code:
```#include <iostream>
const char match = '2';

int main ( void )
{
int x = 12, count = 0;
char buf[BUFSIZ] = {'\0'};
sprintf ( buf, "%d", x );
for ( int i = 0; i < strlen ( buf ); i++ )
if ( buf[i] == match ) count++;
std::cout<< count <<" digit(s) match \'"<< match <<"\'\n";
return 0;
}```
-Prelude

6. or you could use the direct method...
Code:
```int main()
{
int x = 1232, count = 0, target = 2;
while (x) {
if (x % 10 == target)
count++;
x /= 10;
}
...
}```