返回给定字符串中最长连续数字串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
不用任何库函数,系统函数,
完成函数 int maxContinuNum(const char* inputstr,char *outputstr);
返回给定字符串中最长连续数字串,让outputstr指向该串,然后值是其长度。
例如sss12345ss1245sfdf123456789返回,9,outputstr指向123456789。
*/
#include <iostream>
using  namespace  std;
int  maxContunuNum( const  char * inputstr, char  *outputstr)
{
     char  *p=( char  *)inputstr;
     int  max=0,num=0;
     bool  set= false ;
     char  *start;
     while  (*p!= '\0' )
     {
         if ((*p>= '0' )&&(*p<= '9' ))
         {
             if (set== false )
                 start=p; //连续数字中只标记第一个数字的地址
             num++; //记录数出现的次数
             set= true ;
         }
         else
         {
             if (num>max) //如果前面的一串数字是最长的
             {
                 outputstr=start; //则把地址赋给outputstr
                 max=num; //并记下最长的数字串的个数
             }
             set= false ; //为下一轮连续数字做准备
             num=0;
         }
         p++;
     }
     if (num>max)
     { //防止最后一个字符是数字导致没有判断num>max
         outputstr=start;max=num;
     }
     cout<< "最长的数字串为:" <<endl;
     for ( int  i=0;i<max;i++) //输出最长的数字串
         cout<<*(outputstr+i);
     cout<<endl;
     return  max;
}
int  main()
{
     char  *p= "sss12345ss1234sdfaf123456789asfdfs12345678901231a" ;
     char  out[100];
     cout<< "最长连续数字个数为" <<maxContunuNum(p,out)<<endl;
     return  0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值