字符串中找出连续最长的数字串

来源:互联网 发布:电脑做图软件 编辑:程序博客网 时间:2024/05/17 01:33

在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9outputstr所指的值为123456789

#include <stdio.h>int getstr(char* str){    char* pc =NULL;    int count =0;    if(str!=NULL)    {        int i =0;        int t =0;        char*p=NULL;                for(i=0;i<strlen(str);i++)        {            if(('0'<=str[i])&&(str[i]<='9'))            {                if(p==NULL)                {                    p = str+i;                }                t++;                            }            else            {                if(t>count)                {                    count = t;                    pc = p;                }                t =0;                p =NULL;            }        }                if(t>count)        {            count = t;            pc =p;        }                for(i=0; i<count; i++)        {            printf("%c", *(pc + i));        }                printf("\n");    }        return count;}int main(){    char*x ="zbcd12345ad1234567";    printf("count=%d ",getstr(x));         system("PAUSE");    return 0;}


返回:1234567     count=7

时间复杂度为O(n)




 





原创粉丝点击