voidfind_max_number(charstring[],int length,int &count, char *result ){ /* max_length represents the max length of number string. */ int max_length = 0; /* start means first character's position of number string */ int start = 0; /* i represents iterator of string array */ int i = 0; // here we scan the character array. for( ; i < length; i++ ){ // if character belongs to 0~9 we add counter. // otherwise we set counter as 0. if( string[i] >= '0' && string[i] <= '9' ){ count ++; } else{ if( count > max_length ){ max_length = count; start = i - count + 1; } count = 0; } } // finally we should set max_length and the position again. if( count > max_length ){ max_length = count; start = i - count; } // the last, we return counter and the string we need. count = max_length; memcpy( result, &string[start], count*sizeof(char) ); }
intmain( int argc, char ** argv ){ charstring[] = "iabcd12345ed125ss123456789"; char result[20] = {0}; int count = 0; find_max_number( string, strlen(string), count, result ); std::cout << count << " " << result << std::endl; return0; }