删除字符串重复出现的字符函数c语言
时间: 2023-12-11 16:04:16 浏览: 87
以下是删除字符串重复出现的字符的C语言函数的实现:
```c
void removeDuplicates(char *str) {
int len = strlen(str); //获取字符串的长度
if (len < 2) { //如果字符串长度小于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'; //将新字符串的末尾设为'\0'
}
```
使用该函数的示例代码如下:
```c
#include <stdio.h>
#include <string.h>
void removeDuplicates(char *str);
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
removeDuplicates(str);
printf("删除重复字符后的字符串为:%s\n", str);
return 0;
}
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';
}
```
输入字符串后,输出的结果为删除重复字符后的字符串。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)