查询字符串中最长的数字子串程序,欢迎大家评论
#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;
}
...全文
相关推荐
C语言求两个字符串的最长公共子串
主要介绍了C语言求两个字符串的最长公共子串,实例分析了C语言操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
java实现求两个字符串最长公共子串的方法
主要介绍了java实现求两个字符串最长公共子串的方法,是一道华为OJ上的一道题目,涉及Java针对字符串的遍历、转换及流程控制等技巧,需要的朋友可以参考下
python实现求两个字符串的最长公共子串方法
今天小编就为大家分享一篇python实现求两个字符串的最长公共子串方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
查找最长公共子串
查找两个字符串a,b中的最长的公共子串,并将结果输出
输入一个字符串,将输出该字符串最长对称子串及其长度
输入一个字符串,将输出该字符串最长对称子串及其长度,很精巧的算法


