find the longest numeric string in the given input strings

来源:互联网 发布:c语言高级实例解析 编辑:程序博客网 时间:2024/04/29 22:37

题目描述:对于一个输入的字符串,找出全是数字的最长的字符串

/*find teh longest numeric string */#include <stdio.h>#include <cytpe.h>int main(){    char* pos     = NULL;    int maxlen    = -1;    char* curpos  = NULL;    int curlen    = 0;    char str[30]  = {0};    printf("input string\n");    scanf("%s",str);    char *p = curpos = str;    while(*p != 0)    {        if(!isdigit(*p))        {            curlen = 0;            curpos = p;            curpos++;        }        else        {            curlen ++;            if(curlen>maxlen)            {                maxlen = curlen;                pos = curpos;            }        }        p++;    }    if(maxlen)    {        printf("the logest numeric string is:\n");        for(int i=0;i<maxlen;i++)            printf("%c",pos[i]);    }    else        printf("no match result\n");     return 0;}                                                                                                                          
0 0
原创粉丝点击