方法:1、遍历字符串,将重复的字符变为0;
2、根据字符是否为0,刷新字符串
#include <iostream> using namespace std; //让每个字符最多出现一次,删除重复的字符串 void delete_repeted_char(char*p_src) { int length = strlen(p_src); int i, j, n; //把所有重复的字符全变为0 for (i = 0; i < length; i++) { if (!p_src[i]) continue; for (j = i + 1; j < length; j++) { if (p_src[i] == p_src[j]) p_src[j] = 0; } } //根据字符是否为0刷新字符串 for (n = i = 0; i < length; i++) { if (p_src[i] != 0) p_src[n++] = p_src[i]; } p_src[n] = '\0'; } int main() { char src[50]; cout << "输入字符串"; cin >> src; delete_repeted_char(src); cout << src; system("pause"); }