输入: 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. 关于JQueryMobile中Slider的一点研究

    滑动条 Slider                 给input的设置一个新的HTML5属性为type="range",可以给页面添加滑动条组件,可以指定它的value值(当前值 ...

  2. MapReduce程序的工作过程

    转自:http://www.aboutyun.com/thread-15494-1-2.html 问题导读1.HDFS框架组成是什么?2.HDFS文件的读写过程是什么?3.MapReduce框架组成是 ...

  3. 【BZOJ】1058: [ZJOI2007]报表统计(splay+set)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1058 当复习一下splay.... 做法很简单..... 观察得知每一次插入一个点只需要维护前后的绝 ...

  4. poj 1090:The Circumference of the Circle(计算几何,求三角形外心)

    The Circumference of the Circle Time Limit: 2 Seconds      Memory Limit: 65536 KB To calculate the c ...

  5. 【Debian】时间设置

    http://blog.linuxphp.org/archives/567/ http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/ ...

  6. VC++ Debug条件断点使用

    If you're trying to reproduce a rare event and getting too many false positives with your breakpoint ...

  7. 如何用C语言读写文件

    #include "stdio.h"#include <stdlib.h> main(){ FILE *fp1;//定义文件流指针,用于打开读取的文件 FILE *fp ...

  8. UnboundLocalError: local variable 'merchantCode' referenced before assignment

    问题描述:变量赋值前未定义 定位原因:变量没有结果返回,导致赋值失败

  9. Windows远程桌面没有密码的电脑

    你如果想远程一个密码为空的机器,默认情况下是不可以的,需要进行以下设置 1.windows家庭版不支持远程桌面 2. 3.搜索“本地安全策略”

  10. 自定义View总结

    写的很好,代你分析原码,关于 View Measure 测量机制,让我一次把话说完