输入: 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. Linux下tomcat启动

    在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...

  2. 无偏估计(Unbiased Estimator)

    无偏估计是参数的样本估计量的期望值等于参数的真实值. 一个简单的例子(https://www.zhihu.com/question/22983179/answer/23470969): 比如我要对某个 ...

  3. pycharm Tab键设置成4个空格

    file--->setting,选择Editor--->python,如下图所示:

  4. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  5. RabbitMQ OS X下安装及常用命令-1

            RabbitMQ的主页在http://www.rabbitmq.com/ . 1. 安装Erlang RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过 ...

  6. C++ 访问控制 public, protected, private, 友元

    1. 变量属性与继承之间的关系 #include <iostream> using namespace std; class A { public: int x; protected: i ...

  7. 《Windows核心编程》读书笔记.Chapter06线程基础

    原文链接在印象笔记(效果也好的多):https://app.yinxiang.com/l/AAQlNLnxTPRMAppVr5W0upchipQDDC_FHlU 概要: 现成也有两个组成部分: 现成的 ...

  8. IOS7 新特性(针对同样讨厌更新后IOS7的开发者)

    本文转载至 http://blog.csdn.net/hanbing861210/article/details/13614211 您还未登录!|登录|注册|帮助 首页 业界 移动 云计算 研发 论坛 ...

  9. sed使用

    sed命令 一.替换标记 s/pattern/replacement/flags 默认情况下只会替换每行的首次出现的内容,如果要替换其他位置需要使用flags 1.不使用flag [root@bogo ...

  10. 160317(一)、在非action中获取request

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ...