查询字符串中最长的数字子串程序,欢迎大家评论

crystal_avast crystal_avast 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#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;
}

...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
crystal_avast
C++ 语言 65,214 成员 250,514 内容