键盘输入字符串(含数字和字母),输出最长的数字子串和数字个数

#include 

#define MAX_SIZE 1024

int constant_max_num_str(char *dest, char *str)
{
    int i;
    int count = 0;
    int max_len;

    while(*str != '\0')
    {
        while(*str >= '0' && *str <= '9')
        {
            count++;
            str++;
        }
        if(max_len < count)
        {
            max_len = count;
            for(i = 0; i < max_len; i++)
            {
                *(dest + i) = *(str - max_len + i);
            }
            *(dest + i) = '\0';
        }
        count = 0;
        str++;
    }

    return max_len;
}

int main()
{
    char str[MAX_SIZE];
    char dest[MAX_SIZE];

    printf("Please input a string:\n");
    scanf("%s",str);

    int max_len = constant_max_num_str(dest,str);

    printf("The max num string is:%s\nThe len is:%d\n",dest,max_len);

    return 0;
}

你可能感兴趣的:(C语言)