码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-16 21:11:31      阅读:172      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!