输入: hello 输出: helo

第一种实现: 不新开数组, 也就是原地去重.

#include <stdio.h>
#include <string.h> void removeDuplicate(char str[]); int main (void) {
char name[] = "hello";
removeDuplicate(name);
printf("%s\n", name);
return 0;
} void removeDuplicate(char str[]) {
int len = strlen(str);
int p = 0;
int i;
int j;
for (i=0; i<len; i++) {
if (str[i] != '\0') {
str[p++] = str[i];
for (j=i+1; j<len; j++) {
if (str[i] == str[j]) {
str[j] = '\0';
}
}
}
}
str[p] = '\0';
}

上面的代码一共出现了3次'\0', 前2次的'\0'没有什么特殊含义, 可以替换成任何在所给字符串中

不会出现的字符. 最后一个'\0'则是C语言中特有的, 是字符串结束标志.

就是把所有重复的元素标记成'\0', 那么剩下的元素则是不重复的元素, 通过变量p, 把这些元素重新

添加到结果字符串中即可.

第二种实现: 新开数组实现.

#include <stdio.h>
#include <string.h> void removeDuplicate(char str[], char res[]); int main (void) {
char name[20] = "sdfsssww";
char res[20];
removeDuplicate(name, res);
printf("%s\n", res);
return 0;
} void removeDuplicate(char str[], char res[]) {
int slen = strlen(str);
int rlen = 0;
int flag; // 元素重复标志
int i;
int j;
for (i=0; i<slen; i++) {
flag = 0;
for (j=0; j<rlen; j++) {
// 每次都把结果数组遍历一遍, 与当前字符比较, 有重复
// 就标记为 1
if (res[j] == str[i]) flag = 1;
}
if (flag == 0) {
res[rlen++] = str[i];
}
}
res[rlen] = '\0';
}

第三种, 一层循环, 开个ASCII数组进行标记

#include <stdio.h>
#include <string.h> void removeDuplicate(char str[]); int main (void) {
char name[] = "wwwwsssspp";
removeDuplicate(name);
printf("%s\n", name);
return 0;
} void removeDuplicate(char str[]) {
int len = strlen(str);
int ascii[128] = {0};
int p = 0;
int i; for (i=0; i<len; i++) {
if (ascii[str[i]] == 0) {
ascii[str[i]] = 1;
str[p++] = str[i];
}
}
str[p] = '\0';
}

第四种, 也是新开ASCII数组进行标记, 实现去2重, 比如输入: sswqswww, 输出: sswqsw

#include <stdio.h>
#include <string.h> void removeDuplicate(char str[]); int main (void) {
char name[] = "sswqswww";
removeDuplicate(name);
printf("%s\n", name);
return 0;
} void removeDuplicate(char str[]) {
int len = strlen(str);
int ascii[128] = {0};
int p = 0;
int i; for (i=0; i<len; i++) {
if (ascii[str[i]] != 2) {
ascii[str[i]]++;
str[p++] = str[i];
}
}
str[p] = '\0';
}

第五种, 上面的代码简单改下, 既可以实现去n重

#include <stdio.h>
#include <string.h> void removeDuplicate(char str[], int n) int main (void) {
char name[] = "sswqswww";
removeDuplicate(name, 2);
printf("%s\n", name);
return 0;
} void removeDuplicate(char str[], int n) {
int len = strlen(str);
int ascii[128] = {0};
int p = 0;
int i; for (i=0; i<len; i++) {
if (ascii[str[i]] != n) {
ascii[str[i]]++;
str[p++] = str[i];
}
}
str[p] = '\0';
}

参考链接: http://www.hawstein.com/posts/1.3.html

C语言字符串/数组去重的更多相关文章

  1. C语言--- 字符串数组 、 预处理器和预处理指令 、 多文件编程 、 结构体

    1 输入一个姓名,判断是否是五虎上将. 1.1 问题 本案例需要使用交互的方式判断:用户从控制台输入一个名字,由程序判断该名字是否在五虎上将的名单中.五虎上将的名单是:GuanYu.ZhangFei. ...

  2. 字符串数组去重 ["a","b","c","a","b","c"] --> ["a","b","c"]

    非正则实现: let str_arr=["a","b","c","a","b","c&qu ...

  3. go语言实现数组去重

    import ( "fmt" ) func main() { a := []int{2, 1, 2, 5, 6, 3, 4, 5, 2, 3, 9} z := Rm_duplica ...

  4. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  5. C语言学习018:strdup复制字符串数组

    在C语言学习005:不能修改的字符串中我们知道字符串是存储在常量区域的,将它赋值给数组实际是将常量区的字符串副本拷贝到栈内存中,如果将这个数组赋值给指针,我们可以改变数组中的元素,就像下面那样 int ...

  6. C语言字符串与字符数组

    字符串儿与字符数组 字符数组的定义: Char buffer[]; 字符数组初始化: Char buffer1[]="hello world"; 利用scanf输入一个字符串儿 代 ...

  7. 【转】C语言 字符数组与字符串

    原文:http://blog.csdn.net/metasearch/article/details/2856097 在C语言编程中,当我们声明一个字符串数组的时候,常常需要把它初始化为空串.总结起来 ...

  8. JS实现字符串去重,数组去重

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. C语言数组:C语言数组定义、二维数组、动态数组、字符串数组

    1.C语言数组的概念 在<更加优美的C语言输出>一节中我们举了一个例子,是输出一个 4×4 的整数矩阵,代码如下: #include <stdio.h> #include &l ...

随机推荐

  1. easyui datagrid 列拖动

    实现代码-code <script type="text/javascript"> $.extend($.fn.datagrid.methods, { columnMo ...

  2. 学习记录jQuery的animate函数

    很久之前就对jQuery animate的实现非常感兴趣,不过前段时间很忙,直到前几天端午假期才有时间去研究. jQuery.animate的每种动画过渡效果都是通过easing函数实现的.jQuer ...

  3. JAVA 并发编程-多个线程之间共享数据(六)

    多线程共享数据的方式: 1.假设每一个线程运行的代码同样.能够使用同一个Runnable对象,这个Runnable对象中有那个共享数据,比如,卖票系统就能够这么做. 2,假设每一个线程运行的代码不同. ...

  4. hdu 2105:The Center of Gravity(计算几何,求三角形重心)

    The Center of Gravity Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. [Openwrt]wifi桥接设置

    1/连接wifi,用扫描加入,新建一个ingerface  client模式 2/新建wifi ,network选lan和自身新建的interface.此interface设置为bridge模式,连接 ...

  6. 让所有IE支持HTML5的解决方案

    自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注,然而唯一能让IE识别那些新元素(如<article>)的途径是使用HTML5 shiv,感谢remy sharp为我们提 ...

  7. 复习及总结--.Net线程篇(1)

    老是没耐心写这些东西,最近想想也工作两年了,该对自己的东西做个整理了,不知道这次能坚持写几篇,总得来说尽量督促自己吧 言归正传,.net中的多线程主要可以使用两种方法进行调用 1,异步调用 2,Thr ...

  8. ORA-00972: 标识符过长

    若是拼接成的sql语句,请查找传递参数时字符型字段是否两边少了引号.

  9. index封装

    就是求元素在父级当中的位置: 思路: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  10. 【IIS】模块 DLL C:\Windows\System32\inetsrv\authcert.dll 未能加载。返回的数据为错误信息。

    解决方案,check  IIS --Client Certificate Mapping Authentication installed?