#include<iostream>
#include<algorithm>
#include<random>
#include<cmath>
using namespace std;
int main()
{
string str;
cout << "请输入一个字符串:";
cin >> str;
int len = str.length();
for (int i = 0; i < len; i++)
{
for (int j = i+1; j < len; j++)
{
if (str[i] == str[j])
{
str[j] = '\0';
}
}
}
for (int j = 0; j < len; j++)
{
if (str[j] == '\0')
continue;
else
cout << str[j];
}
}
首先我用for循环来找到重复字母的下标,然后再将其元素设置为空字符,
然后再利用一个for循环去读取string,在遇到空的时候不打印,直接continue