在字符串中找出连续最长的数字串,并把这个串的长度返回

C语言 码拜 9年前 (2015-05-12) 960次浏览 0个评论
 

#include <stdlib.h>
#include”string”
using namespace std;
#include “oj.h”

/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
   unsigned int Continumax(char** pOutputstr,  char* intputstr)
输入参数:
   char* intputstr  输入字符串
输出参数:
   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
   pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放

返回值:
  连续最长的数字串的长度

 */
unsigned int Continumax(char** pOutputstr,  char* intputstr)
{
int Len=strlen(intputstr);
if (*intputstr==NULL || Len==0)
{
*pOutputstr=””;
return 0;
}

int MaxLen=0;
int wordLen=0;
int i,j,begin=0;
int flag[200];
for (i=0;intputstr[i]!=””\0″”;i++)           //判断字符是否为数字
{
if (intputstr[i]>””0″” && intputstr[i]<“”9″”)
flag[i]=1;
else flag[i]=0;
}
for (i=0;i!=””\0″”;)
{
if (flag[i]==1)
{
for(j=i;flag[j]=1;j++)   //获得数字串的长度wordLen
wordLen++;
if (wordLen>=MaxLen)
{
MaxLen=wordLen;
begin=i;
}
i=i+wordLen;
}
else 
i++;
}

    char *temp = (char*)malloc((MaxLen+1)* sizeof(char)); 
for (i=0;i<MaxLen;i++)  
    {  
        temp[i]=intputstr[begin+i];  //不改变outputstr地址    
    }  
temp[MaxLen]=””\0″”;
//Ptr=Ptr-maxLen;
        
*pOutputstr=temp;
return MaxLen;
}

40分
unsigned int Continumax(char** ppout,  char* src)
{
    char *last=NULL;
    int ans=0;
    for(;*src;++src){
        if(isdigit(*src)){
            if(!last) last =src;
        }else{
            if(last&&src-last>ans){
                *ppout =last;
                ans =src-last;
                last =NULL;
            }
        }
    }
    if(last && src-last>ans)
    {
        *ppout =last;
        ans =src-last;
    }
    return ans;
}
 
自己顶一下~
Maxlen结果正确,但是赋值不对~
 
错误应该在这部分
 char *temp = (char*)malloc((MaxLen+1)* sizeof(char)); 
for (i=0;i<MaxLen;i++)  
    {  
        temp[i]=intputstr[begin+i];  //不改变outputstr地址    
    }  
temp[MaxLen]=””\0″”;
//Ptr=Ptr-maxLen;
        
*pOutputstr=temp;
return MaxLen;
}
 
咨询了下,同学不是指针的错误~前面的表达有欠妥当,把源码粘出来

#include <stdlib.h>
#include”string”
using namespace std;
#include “oj.h”

/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
   unsigned int Continumax(char** pOutputstr,  char* intputstr)
输入参数:
   char* intputstr  输入字符串
输出参数:
   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
   pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放

返回值:
  连续最长的数字串的长度

 */
unsigned int Continumax(char** pOutputstr,  char* intputstr)
{
int Len=strlen(intputstr);
if (*intputstr==NULL || Len==0)
{
*pOutputstr=””;
return 0;
}

int MaxLen=0;
int wordLen=0;
int i,j,begin=0;
int flag[200];
for (i=0;intputstr[i]!=””\0″”;i++)           //判断字符是否为数字
{
if (intputstr[i]>=””0″” && intputstr[i]<=””9″”)
flag[i]=1;
else flag[i]=0;
}
for (i=0;intputstr[i]!=””\0″”;)
{
if (flag[i]==1)
{
for(j=i;flag[j]==1 && intputstr[j]!=””\0″”;j++)   //获得数字串的长度wordLen
wordLen++;
if (wordLen>=MaxLen)
{
MaxLen=wordLen;
begin=i;
}
i=i+wordLen;
}
else 
i++;
wordLen=0;
}

    char *temp = (char*)malloc((MaxLen+1)* sizeof(char)); 
for (i=0;i<MaxLen;i++)  
    {  
        temp[i]=intputstr[i+begin];  //不改变outputstr地址    
    }  
temp[MaxLen]=””\0″”;
//Ptr=Ptr-maxLen;
        
*pOutputstr=temp;
return MaxLen;
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在字符串中找出连续最长的数字串,并把这个串的长度返回
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!