首页 > 其他 > 详细

求一个字串中最长的连续字符串

时间:2014-06-16 21:10:59      阅读:490      评论:0      收藏:0      [点我收藏+]

举例子来说:对于字符串"1234abcdef1234567abcdefghijklmn",这个字串中最长的连续字符串为“abcdefghijklmn”。




int continumax(char *outputstr,char *inputstr)
{
	char maxrecord[100] = {0};
	int maxlength = 0;

	char currentrecord[100] = {0};
	int currentlength = 0;

	char value = 0;
	char *p = inputstr;
	
	while(*p++ != '\0')
	{
		if (value + 1 == *p)
		{
			value = *p;
			currentrecord[currentlength] = *p;
			currentlength++;
		}
		else
		{
			if (currentlength > maxlength)
			{
				maxlength = currentlength;
				memcpy(maxrecord, currentrecord, currentlength);
			}

			currentlength = 0;
			memset(currentrecord, 0, 100);
			
			value = *p;
			currentrecord[currentlength] = *p;
			currentlength++;

		}
	}

	if (currentlength > maxlength)
	{
		maxlength = currentlength;
		memcpy(maxrecord, currentrecord, currentlength);
	}

	memcpy(outputstr, maxrecord, maxlength);
	return maxlength;
}





求一个字串中最长的连续字符串,布布扣,bubuko.com

求一个字串中最长的连续字符串

原文:http://blog.csdn.net/wayneforever/article/details/30986637

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!