找出最长的数字串(和找出句子中最长的单词一样的方法)

#include <iostream>
using namespace std;

//找到字符串中最长的数字串
void find_max_num_string(char *str,char max[] )
{
    char c;
    char num[50] = { "\0" };
    int i=0; int j=0;
    int new_sign= 0;
    while(1)
    {
        c= str[i++];
        if (c == '\0')
        {
            if (new_sign == 1)
            {
                num[j] = '\0';
                if (strlen(num) > strlen(max))

                    memcpy(max, num,  strlen(num));
            }
            break;
        }
        else
        {
            if (c <= '9'&&c >= '0'&&new_sign == 0)
            {
                num[j++] = c;
                new_sign = 1;
            }

        
            else if (c <= '9'&&c >= '0'&&new_sign == 1)
            {
                num[j++] = c;
            }
            else if (c > '9' || c < '0'&&new_sign == 1)
            {
                num[j] = '\0';
                if (strlen(num) > strlen(max))
                {
                    memcpy(max, num, strlen(num));
                }
                j = 0;
                new_sign = 0;
            }
        }


    }
}
int main()
{
    char src[50] = { '\0' }; char word[50] = { '\0' };
    cout << "输入字符串";
    cin.getline(src, 50);
    find_max_num_string(src, word);
    cout << word;
    system("pause");
}

 

posted @ 2018-04-16 23:20  新新苦苦的学习  阅读(210)  评论(0编辑  收藏  举报