is there any way to search for the occurence of an integer inside an iteger? ie. to try to find '2' in '12' ?
Thanks
is there any way to search for the occurence of an integer inside an iteger? ie. to try to find '2' in '12' ?
Thanks
you could copy that integer to a char and then search.
I dont know of any direct method.
-
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.
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
orCode:#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; }
-PreludeCode:#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; }
My best code is written with the delete key.
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; } ... }
Last edited by *ClownPimp*; 05-09-2002 at 07:29 PM.