Find a character and position that appears only once in the string

struct str_info
{
	unsigned int idx;
	unsigned int cnt;
	unsigned char val;
	str_info()
	{
		idx = 0;
		cnt = 0;
	}
};
int FirstNotRepeatingChar(string str) {
	int len = str.length();
	if(len < 0 || len > 10000)
		return 0;
	char *p = &str[0];
	int id = 0;
	str_info str_myInfo[0x80];
	while(*p != '\0')
	{
		str_myInfo[*p].idx = id++;
		str_myInfo[*p].cnt++;
		str_myInfo[*p].val = *p;
		p++;
	}

	char *pStr = &str[0];
	while((*pStr) != '\0')
	{
		if(str_myInfo[*pStr].cnt == 1)
		{
			return str_myInfo[*pStr].idx;
		}
		pStr++;
	}
	return 0;
}

Mainly use the character itself as a subscript of an array to record the number of occurrences and its position in the entire string.

Published 42 original articles · praised 4 · 10,000+ views

Guess you like

Origin blog.csdn.net/wangyhwyh753/article/details/79175013