find the longest numeric string in the given input strings

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

/*find teh longest numeric string */
#include 
#include 
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;iprintf("%c",pos[i]);
    }
    else
        printf("no match result\n"); 
    return 0;
}                                                                                                                          

你可能感兴趣的:(codewars,C/C++,code-for-f)