问题:挑选出字符序列中出现次数为奇数的字符,然后按照ascii码顺序输出这些字符
下面这个代码可以找出出现奇数次的字符,但不能顺利排序去重,想知道自己的问题在哪里呢
void OddFilter(char a[], int size) {
int b[1000];//用于每个字符出现的次数
for (int i = 0; i < size; i++) {
int sum = 0;
char p = a[i];
for (int j = 0; j < size; j++) {
if (p == a[j]) { ++sum; }
}
b[i] = sum;
}
char location[1000] = {'\0'};//用于储存字符
int x = 0;
for (int i = 0; i < size; i++) {
if (b[i] % 2 != 0) { //如果是奇数
location[x] = a[i];//将A中对应字符储存进location
x++;
}
}//出现奇数次的储存进location
for (int i = 0; i < x-1; i++) {
for (int q = 0; q >= x - i-1; x++) {
if (location[q + 1] < location[q]) {
char temp = location[q];
location[q] = location[q + 1];
location[q + 1] = temp;
}
}
}//排序
for (int i = 0; location[i+1] != '\0'; i++) {
if (location[i] == location[i + 1]) {
location[i + 1] = location[i + 2];
}
}//去重
int l = strlen(location);
cout << location[0];
for (int i = 1; i < l; i++) {
cout << " " << location[i];
}
}
输入:Thisisateststring
运行结果:T h i a t e t t r i n g
我想要达到的结果:T a e g h i n r t
##刚开始学,感谢帮助!