【练习】写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,

/************************************************************************/
/* 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,
并把这个最长数字串付给其中一个函数参数outputstr 所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr 后,函数将返回9,
outputstr 所指的值为123456789
*/
/************************************************************************/
#include <iostream>
 
using namespace std;
 
int continumax(char *outputstr,char *intputstr)
{
    int length=0,curLength=0;;
    char *temp=NULL;
    while((*intputstr)!='\0')
    {
        while(isdigit(*intputstr)&&(*intputstr)!='\0')
        {
            if (temp==NULL)
            {
                temp=intputstr;
            }  
            curLength++;
            intputstr++;
        }
        if (curLength>length)
        {
            length=curLength;
            outputstr=temp;
            temp=NULL;
            curLength=0;
        }
        else
        {
            temp=NULL;
            curLength=0;
        }
        intputstr++;
 
    }
    for (int i=0;i<length;i++)
    {
        cout<<*outputstr++;
    }
    cout<<endl;
    return length;
}
 
int main()
{
    char *intputstr="abcd12345ed125ss123456789";
    char *outputstr=NULL;
    int length=continumax(outputstr,intputstr);
    cout<<length;
     
 
    return 0;
}
posted @ 2012-08-28 16:22  夏日冰茶  阅读(461)  评论(0编辑  收藏  举报