,获取一个字符串的最大数字子字符串
高手请进,获取一个字符串的最大数字子字符串
题目描述
请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;
注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!
样例输入
abcd12345ed125ss123058789
abcd12345ss54761
样例输出
输出123058789,函数返回值9
输出54761,函数返回值5
接口说明
函数原型:
unsignedint Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串;
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;
返回值:
连续最长的数字串的长度
这是我写的代码,但部分用例无法通过,比如我输入“12”,按道理将应该返回“12” 和 2.但是程序会报错,错误信息是:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: D:\C++测试\testc\Debug\testc.exe
HEAP CORRUPTION DETECTED: after Normal block (#56) at 0x005A14F8.
CRT detected that the application wrote to memory after end of heap buffer.
(Press Retry to debug the application)
---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------
我不知道是什么原因造成的,高手看看!看代码还有没有其他问题!
我的代码:
------解决方案--------------------
那就改这里一下:
tempStr = (char*)malloc(length + 2);
memset(tempStr,0,sizeof(length + 2));
具体那里访问越界了,可以慢慢跟。
题目描述
请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;
注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!
样例输入
abcd12345ed125ss123058789
abcd12345ss54761
样例输出
输出123058789,函数返回值9
输出54761,函数返回值5
接口说明
函数原型:
unsignedint Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串;
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;
返回值:
连续最长的数字串的长度
这是我写的代码,但部分用例无法通过,比如我输入“12”,按道理将应该返回“12” 和 2.但是程序会报错,错误信息是:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: D:\C++测试\testc\Debug\testc.exe
HEAP CORRUPTION DETECTED: after Normal block (#56) at 0x005A14F8.
CRT detected that the application wrote to memory after end of heap buffer.
(Press Retry to debug the application)
---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------
我不知道是什么原因造成的,高手看看!看代码还有没有其他问题!
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned char isnumber(char ch)
{
if((ch < '0') || (ch > '9'))
return 0;
return 1;
}
char* getnumpos(char* str)
{
char ch;
while(ch = *str)
{
if(!isnumber(ch))
++str;
else
break;
}
return str;
}
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
unsigned int length;
char* tempStr = NULL;
char ch;
unsigned int count = 0;
if((NULL == pOutputstr) || (NULL == intputstr))
return -1;
if((length = strlen(intputstr)) == 0)
{
**pOutputstr = '\0';
return 0;
}
tempStr = (char*)malloc(length + 1);
memset(tempStr,0,sizeof(length + 1));
intputstr = getnumpos(intputstr);
while(ch = *intputstr++)
{
if(!isnumber(ch))
{
*(tempStr + count) = '\0';
if(strlen(tempStr) >= strlen(*pOutputstr))
{
memcpy(*pOutputstr,tempStr,length + 1);
}
memset(tempStr,0,length + 1);
count = 0;
intputstr = getnumpos(intputstr);
}
else
{
*(tempStr + count ++) = ch;
}
}
*(tempStr + count) = '\0';
if(strlen(tempStr) >= strlen(*pOutputstr))
{
memcpy(*pOutputstr,tempStr,length + 1);
}
free(tempStr);
tempStr = NULL;
return strlen(*pOutputstr);
}
int main(void)
{
int n = 0;
char* p = (char*)malloc(100);
memset(p,0,100);
n = Continumax(&p,"12");
printf("%s : %d\n",p,n);
free(p);
return 0;
}
------解决方案--------------------
那就改这里一下:
tempStr = (char*)malloc(length + 2);
memset(tempStr,0,sizeof(length + 2));
具体那里访问越界了,可以慢慢跟。