处理字符串"C1, C2, C3,C99, C100, C102,C88"成"C1-C2,C99-C102,C88"总结

假设有这样一些字符串:

 	char *str = "C1,C10,C11, C100, C101, C102, C12,C17,C54,C55,C56,C57,C58,C59,"
 				"C60,C61,C62,C63,C64,C65,C66,C67,C68,C69,C70,C71,C72,C73,C74,"
 				"C75,C76,C77,C78,C79,C80,C81,C82,C83,C84,C85,C86,C87,C88";

处理成:

C1, C10-C12,C17,C54-C88,C100-C102

的方法,写的有些笨,但还是有能用的地方:

处理字符串"C1, C2, C3,C99, C100, C102,C88"成"C1-C2,C99-C102,C88"总结_第1张图片

#include <stdio.h>
#include <string.h>
#include <vector>
#include <ctype.h>

using namespace std ;

void GetPrefixStr(char *strIn, char *strOut);
void GetFormatStr(const char *strIn, const char *strPrefix, char *strOut);
void GetEveryContinueStr(const char *strIn, char *strOut);
char *MyStrStr(const char *strSource, const char *strDest);
void GetFristWord(const char *strIn, char *strWord);



void main()
{
	char strPrefix[100] = {0};
	char buf[100] = {0};

	//设一个电容C开头的字符串数组(下面是连续的一行串)
 	//char *str = "C1,C10,C11, C100, C101, C102, C12,C17,C54,C55,C56,C57,C58,C59,"
 	//			"C60,C61,C62,C63,C64,C65,C66,C67,C68,C69,C70,C71,C72,C73,C74,"
 	//			"C75,C76,C77,C78,C79,C80,C81,C82,C83,C84,C85,C86,C87,C88";

	//设一个LED开头的字符串数组
	char *str = "LED600,LED60,LED10,LED11, LED100, LED101, LED102, LED12,LED17,LED54,LED55,LED56,LED57,LED58,LED59";


	GetFristWord(str, buf);			//拷贝出第一个单词
	GetPrefixStr(buf, strPrefix);	//得到前缀字母
 	printf("前缀: %s\n", strPrefix);


	char strOut[1000] = {0};
	GetFormatStr(str, strPrefix, strOut);
	printf("处理完毕: %s\n", strOut);

}

//
// 改造过的字符查找,主要是针对匹配整个,如源串"C10",在目标串"C101 C102"中不能算查找成功!
//
char *MyStrStr(const char *strSource, const char *strDest)
{
	char *tmp = (char *)strSource;
	int len = strlen(strDest);

	while(1)
	{
		tmp = strstr(tmp, strDest);
		if (!tmp) break;

		if(tmp[len] == '\0' ||
			tmp[len] == ',' ||
			tmp[len] == ' ')
			return tmp;
		
		tmp += len;
	}

	return NULL;
}


void GetFormatStr(const char *strIn, const char *strPrefix, char *strOut)
{
	if(strIn == NULL || strPrefix == NULL || strOut == NULL)
		return;

#define MAX_IN_BUF	(1024)
#define MAX_LEN		(1024)

	int i = 0;
	int j = 0;
	char strWord1[100] = {0};
	char strWord2[100] = {0};
	char strTmp[1000] = {0};
	char strTmp2[1000] = {0};
	char strTmp3[1000] = {0};

	for(i=0; i<MAX_LEN; i++)	//默认认为不会超过1000个逗号分割的串
	{
		sprintf(strWord1, "%s%d", strPrefix, i);
		if(MyStrStr(strIn, strWord1))
		{
			//添加当前要处理的一个
			strcpy(strTmp, strWord1);
			strcat(strTmp, ",");

			//开始循环当前一个单词能匹配上的
			int cur = i;
			for(j=0; j<MAX_LEN; j++)
			{
				sprintf(strWord2, "%s%d", strPrefix, j);
				if(MyStrStr(strIn, strWord2))
				{
					if(j-cur == 1)
					{
						strcat(strTmp, strWord2);
						strcat(strTmp, ",");
						i = cur = j;
					}
				}
			}


			//找到了当前匹配上的一组单词,开始处理这一组
			//printf("%s\n", strTmp);
			strTmp2[0] = '\0';
			GetEveryContinueStr(strTmp, strTmp2);

			//处理完这一组单词,保存下来
			strcat(strTmp3, strTmp2);
			strcat(strTmp3, ",");
		}
	}

	//去掉最后一个逗号
	int num =  strlen(strTmp3);
	if (strTmp3[num-1] == ',')
	{
		strTmp3[num-1] = '\0';
	}
	strcpy(strOut, strTmp3);
}


void GetFristWord(const char *strIn, char *strWord)
{
	char buf[100] = {0};
	strncpy(buf, strIn, 100); //假设一个word不会超过100个字节长

	char *tmp = (char *)buf;
	while(1)
	{
		if(*tmp == ',' ||
			*tmp == ',' ||
			*tmp == '\0')
		{
			break;
		}
		tmp++;
	}

	*tmp = '\0';
	strcpy(strWord, buf);
}

//
// 得到如"C100, C102"中的字符"C"
//
void GetPrefixStr(char *strIn, char *strOut)
{
	char buf[100] = {0};
	strcpy(buf, strIn);
	char *tmp = buf;
	while(1)
	{
		if (isdigit(*tmp))
		{
			*tmp= '\0';
			strcpy(strOut, buf);
			return ;
		}
		if(*tmp == '\0')
			break;

		tmp++;
	}

	strcpy(strOut, buf);
}

//
// 处理"C10, C11, ..., C89"成 "C10-C89"
//
void GetEveryContinueStr(const char *strIn, char *strOut)
{
	char tmp[100] = {0};
	char seps[]   = " ,";
	char *token = NULL;
	vector<char *> s;
	token = strtok( (char *)strIn, seps );
	while( token != NULL )
	{
		s.push_back(token);
		token = strtok( NULL, seps );
	}

	if(s.size() == 1)
	{
		strcpy(strOut, strIn);
		return;
	}
	else
	{
		strcpy(strOut, s[0]);
		strcat(strOut, "-");
		strcat(strOut, s[s.size()-1]);
	}
}


你可能感兴趣的:(处理字符串"C1, C2, C3,C99, C100, C102,C88"成"C1-C2,C99-C102,C88"总结)