代码如下(建议使用C++编译器编译或支持最新C标准的编译器,我在C11标准下通过):
#include
#include
#include
void Trim(char *str) {
char *strTmp = (char *)malloc(sizeof(char) * strlen(str));
int i = 1, j = 1;
while (str[j] != '\0') {
if (str[j] != str[j - 1]) {
strTmp[i - 1] = str[j - 1];
i++;
j++;
} else {
j++;
}
}
strTmp[i - 1] = str[j - 1];
strcpy(str, strTmp);
free(strTmp);
}
int main() {
char *str1 = "a3B2c1";
char *str2 = "Q5BcF570";
char *str3 = (char *)malloc(sizeof(char) * (strlen(str1) + strlen(str2)));
strcpy(str3, str1);
strcat(str3, str2);
for (int i = 0; i < strlen(str3); i++)
for (int j = 0; j < i; j++)
if (str3[i] < str3[j]) {
char cTmp = str3[i];
str3[i] = str3[j];
str3[j] = cTmp;
}
Trim(str3);
printf("%s + %s => %s\n", str1, str2, str3);
free(str3);
return 0;
}