C语言实现去掉字符串中重复的字符

写一个函数,去掉一个字符串中重复的字符,比如将"goole"转成"gole"

char * str_uniq(char* str)
{
     
	int i = 0;
	int j = 0;
	int k = 0;
	while(str[i] != '\0')
	{
     
		j = i + 1;
		while(str[j] != '\0')
		{
     
			if(str[i] == str[j])
			{
     
				k = j;
				while(str[k] != '\0')
				{
     
					str[k] = str[k+1];
					k++;
				}
				str[k] = '\0';
				j--;
			}
			j++;
		}
		i++;
	}
	return str;
}

int main(void)
{
     
	char str[] = "goolegoole";
	str_uniq(str);
	printf("%s\n", str);	//gole
	return 0;
}

你可能感兴趣的:(笔试常考点,字符串,c语言)