64,643
社区成员
发帖
与我相关
我的任务
分享
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;
struct strNode
{
const char* pStr;
int nMax;
strNode():pStr( NULL ), nMax( 0 )
{
}
};
bool isNumChar( const char* pStr )
{
if ( ( *pStr == '0' ) || ( *pStr == '1' ) || ( *pStr == '2' )
|| ( *pStr == '3') || ( *pStr == '4' ) || ( *pStr == '5')
|| ( *pStr == '6') || ( *pStr == '7' ) || ( *pStr == '8')
|| ( *pStr == '9' ))
{
return true;
}
else
{
return false;
}
}
void FindMaxNumSubString( string& str, strNode& strNum )
{
const char* pStr = str.c_str();
int nMax;//= 0;
const char* pStrTemp = NULL;
while ( NULL != *pStr )
{
if ( isNumChar( pStr ))
{
nMax = 0;
nMax++;
pStrTemp = pStr;
while(( ++pStr ) && ( NULL != *pStr ))
{
if ( isNumChar( pStr ))
{
nMax++;
}
else
{
++pStr;
break;
}
}
if ( strNum.nMax < nMax )
{
strNum.nMax = nMax;
strNum.pStr = pStrTemp;
}
}
else
{
++pStr;
}
}
}
int main( int argc, char* argv[] )
{
string str( "abc2012dcb201jhg2012980221bcd12k" );//""
strNode strNum;
FindMaxNumSubString( str, strNum );
char* a = new char[strNum.nMax+1];// = { 0 };
memset( a, 0, strNum.nMax+1 );
str.copy( a, strNum.nMax, str.length() - string( strNum.pStr ).length() );
cout << a << endl;
_getch();
return 0;
}