现在的位置: 首页 > 综合 > 正文

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

2019年01月09日 ⁄ 综合 ⁄ 共 685字 ⁄ 字号 评论关闭

举例子来说:对于字符串"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;
}

抱歉!评论已关闭.