匹配字符串小工具

函数作用就是匹配字符串是否是源字符串相似的字符串

参数,strPrimary 是模板字符串  , strMatches是需要匹配的字符串.

例如:  strPrimary字符串是MMAPLFA字样, strMatches字符串是MAP_FAMB035, 需要匹配的字符串和模板字符串有5个字母像匹配,分别是MAP和FA而且这几个字符相对位置也是一样的.函数返回5.

int StringMatches(const std::string& strPrimary, const std::string& strMatches)
{
	int max = 0, num, i, j;
	for (int k = strPrimary.size() - 1; k > 0; k--)
	{
		num = 0;
		i = k;
		j = 0;
		while (1)
		{
			if (i >= strPrimary.size() || j >= strMatches.size())
			{
				break;
			}
			if (strPrimary.at(i++) == strMatches.at(j++))
			{
				num++;
			}
		}
		max = (num > max) ? num : max;
	}
	for (int k = 0; k < strMatches.size(); k++)
	{
		num = 0;
		i = 0;
		j = k;
		while (1)
		{
			if (i >= strPrimary.size() || j >= strMatches.size())
			{
				break;
			}
			if (strPrimary.at(i++) == strMatches.at(j++))
			{
				num++;
			}
		}
		max = (num > max) ? num : max;
	}
	return max;
}

你可能感兴趣的:(C++知识,c++)