《每日一題》字符串中找出連續最長的數字串

題目描述

讀入一個字符串str,輸出字符串str中的連續最長的數字串

輸入描述:

個測試輸入包含1個測試用例,一個字符串str,長度不超過255。

輸出描述:

在一行內輸出str中裏連續最長的數字串。
示例1

輸入

abcd12345ed125ss123456789

輸出

123456789

代碼

#include<stdio.h>
#include<string.h>

void Deal(char *str)
{
    int p=0;
    char* start;
    int end;
    int len=0;
    int max=0;
    int lenlen=strlen(str);
    if(str==NULL)
        return;
     while( p<lenlen)
     {
         if(str[p]>='0'&&str[p]<='9')
         {
             end=p;
             len=0;
             while(str[end]>='0'&&str[end]<='9')
             {
                 len++;
                 end++;
             }
             if(max<len)
             {
                 max=len;
                 start=&str[p];
             }
             p=end;
         }
         else
         {
             p++;
         }
     }
      while(max--)
      {
          printf("%c",*start++);
      }
}


int main()
{
        char str[256];
    gets(str);
    Deal(str);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章