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

crystal_avast 2012-02-21 07:17:13

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

...全文
135 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
IVERS0N 2012-02-22
  • 打赏
  • 举报
回复
学习下
crystal_avast 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 last_man 的回复:]

新手问问const char* pStr = str.c_str();
c_str()是什么??
[/Quote]

可以查看参考手册
LAST_MAN 2012-02-21
  • 打赏
  • 举报
回复
新手问问const char* pStr = str.c_str();
c_str()是什么??
quwei197874 2012-02-21
  • 打赏
  • 举报
回复
动态归化的经典使用案例就是这个算法,楼主可以去参考下
crystal_avast 2012-02-21
  • 打赏
  • 举报
回复
沙发,保证可以运行

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧