用c语言编写一个程序去掉给定字符串中重复的字符。
下面是使用C语言编写的一个程序,可以去掉给定字符串中重复的字符:
#include <stdio.h>
#include <string.h>
void removeDuplicates(char* str) {
int len = strlen(str);
if (len < 2) {
return;
}
int tail = 1;
for (int i = 1; i < len; i++) {
int j;
for (j = 0; j < tail; j++) {
if (str[i] == str[j]) {
break;
}
}
if (j == tail) {
str[tail] = str[i];
tail++;
}
}
str[tail] = '\0';
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
removeDuplicates(str);
printf("去重后的字符串:%s\n", str);
return 0;
}
你可以通过在main函数中输入一个字符串来测试程序。程序会去除字符串中的重复字符,并输出结果
原文地址: https://www.cveoy.top/t/topic/iapB 著作权归作者所有。请勿转载和采集!