除掉字符串里面相同的字符
【摘要】 题目:
除掉字符串里面相同的字符
such as 字符串“aaaabbbccdd” 出去相同的字符就会变成“abcd”
代码:
#include <stdio.h>#include <stdlib.h>#include <string.h> void grepString1(char a[]) { i...
题目:
除掉字符串里面相同的字符
such as 字符串“aaaabbbccdd” 出去相同的字符就会变成“abcd”
代码:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
void grepString1(char a[]) {
-
int length = strlen(a);
-
int i = 0, j = 0;
-
for (i=0; i < length; ++i) {
-
if (a[i] != 0) {
-
a[j++] = a[i];
-
for (int k = i+1; k < length; k++) {
-
if (a[i] == a[k]) {
-
a[k] = 0;
-
}
-
}
-
}
-
}
-
}
-
-
void grepString2(char a[]) {
-
bool h[256];
-
memset(h, 0, sizeof(h));
-
int i = 0, j = 0;
-
while (a[i]) {
-
if (!h[a[i] - 'a']) {
-
a[j++] = a[i];
-
h[a[i] - 'a'] = true;
-
}
-
++i;
-
}
-
a[j] = 0;
-
}
-
-
int main() {
-
char a[10] = "aaabbccdd";
-
grepString1(a);
-
printf("字符串里面不同的字符为%s\n", a);
-
char str[100] = "aaabbccdddddfffffdaaceeedj";
-
grepString2(str
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/52799649
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)