输入: 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. Hibernate Annotation 字段 默认值

    http://emavaj.blog.163.com/blog/static/133280557201032262741999/ ——————————————————————————————————— ...

  2. libcurl库的编译

    终于弄懂了libcurl库的编译,记下来免得忘记. 下载地址:   libcurl库:http://curl.haxx.se/latest.cgi?curl=zip   openssl安装包:http ...

  3. PHP 汉字转成拼音

    <?php class ZH{ /** * 将字符串转化为拼音 */ function Pinyin($_String, $_Code='gb2312') { $_DataKey =" ...

  4. C++ 指针引用

    //指针引用 #include<iostream> using namespace std; struct Teacher{ ]; int age; }; int InitA(Teache ...

  5. 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...

  6. HDU1717--小数化分数2

    这道题是将输入的小数(有可能是无限循环小数)来化为分数.刚開始看到以为枚举(千万不要嘲笑我),可是感觉不正确. 所以百度了小数化为分数的方法,然后看到了各种方法,原来是这这样,在这我採用的是小数化为分 ...

  7. PHP中strlen和mb_strlen函数的区别

    strlen strlen — 获取字符串长度 int strlen ( string $string ) 返回给定的字符串 string 的长度. mb_strlen int mb_strlen ( ...

  8. SpringCloud服务发现(Eureka)简介

    Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能. 为什么要使用Eure ...

  9. leetcode -- Maximal Rectangle TODO O(N)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  10. UE4读取配置文件里面的key-value

    在MyProject/Config/DefaultGame.ini配置文件中添加 [RamaUDP]listenPort=2017PrintLog=false C++代码读取 int32 listen ...