读入一个字符串str,输出字符串str中的连续最长的数字串

读入一个字符串str,输出字符串str中的连续最长的数字串 
输入描述:

测试输入包含1个测试用例,一个字符串str,长度不超过255。

输出描述:

在一行内输出str中里连续最长的数字串。

输入例子:  abcd12345ed125ss123456789

输出例子:123456789

解题思路

  • 首先遍历字符串,找到连续的数字串
  • 把这一部分数字串遍历完后,记录数字串长度,继续往后遍历
  • 与后面的数字串长度进行比较,如果大于之前的就更新最大数字串
  • 输出刚刚标记的数字串 

代码

#include<iostream>
#include<string>
using namespace std;

void Findnum(string str)
{
	size_t i = 0;
	int count = 0;//计数
	int maxnum = 0;//标记最大数字串长度
	int pos = 0;//最大数字串的开始位置
	while (i < str.size())
	{
		while (!isdigit(str[i]))
			i++;//不是数字就往后走
		while (isdigit(str[i]))
		{
			count++;
			i++;
		}
		if (count>maxnum)
		{
			maxnum = count;//更新maxnum
			pos = i - maxnum;//标记pos
		}
		count = 0;
	}
	for (int j = pos; j < pos+maxnum; j++)
	{
		cout << str[j] ;
	}
}

int main()
{
	string str="abcd12345ed125ss123456789";
	Findnum(str);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值