程序要求如下:
一个字符串,字符连续的部分为子串(不含带数字的部分)
下面是我写的程序
#include
using namespace std;
char* BiggestStr(char* str)
{
char* pStr = (char*)malloc(sizeof(str)+1); //申请一个空间存最大子串
int nCount = 0; //用于记录最大子串的长度
int J_nCount=0;
char* J_pStr = (char*)malloc(sizeof(str)+1);
//遍历传进来的字符串。。。找最大子串
while(*str !='\0')
{
//找到属于字符串的那一段,把它存到申请的空间内,并且记录个数
if((*str) >= 'a' && (*str) <= 'z')
{
nCount++;
*pStr = *str;
pStr++;
}
else
{
//如果找到更大的子串 把他们标记下来
if(nCount > J_nCount)
{
J_nCount = nCount;
strcpy_s(J_pStr,J_nCount+1,pStr);
}
//标记后清0 继续寻找更大的子串
nCount = 0;
pStr = NULL;
}
str++;
}
return J_pStr;
}
int main()
{
char* str = "abc123";
char* pStr = BiggestStr(str);
cout<<pStr<<endl;
system("pause");
return 0;
}
我下了断电调试了下,在把pStr复制给J_pStr的时候出现错误。。
我看了是pStr里面的东西显示没有请问这是为什么啊