#c语言编程#实现将字符串中所有重复的字符剔除,将剔除之后的输出 如sajsk输出ajk

如题所述

第1个回答  2017-01-14
#include <stdio.h>
#include <string.h>

void sort(char s[]) { // 选择排序
int i,j,k,t,len = strlen(s);
for(i = 0; i < len - 1; ++i) {
k = i;
for(j = i + 1; j < len; ++j) {
if(s[k] < s[j]) k =j;
}
if(i != k) {
t = s[i];
s[i] = s[k];
s[k] = t;
}
}
}

// 将不重复的字符复制到t[],返回删除的字符个数
int change(char s[], char t[]) {
int i = 1,j = 0,cnt = 0;
sort(s);
t[0] = s[0];
while(s[i]) {
if(s[i] == s[i - 1]) ++cnt;
else t[++j] = s[i];
++i;
}
t[j] = '\0';
return cnt;
}

int main() {
char s[] = "aseqkwh wkqhasweewwqbkh112504ffvsdr";
char t[60];
printf("原串:%s\n",s);
printf("共删除%d个字符。\n",change(s,t));
printf("最后得到的字符串为:%s\n",t);
return 0;
}本回答被提问者采纳
第2个回答  2017-01-14
public static void test1()
        {
            string testStr = "你好你好,中国,hello,world,";

            char[] charArr = testStr.ToCharArray();
            HashSet<char> charSet = new HashSet<char>();
            HashSet<char> repeatCharSet = new HashSet<char>();
            for (int i = 0; i < charArr.Length; ++i)
            {
                if (charSet.Contains(charArr[i]))
                {
                    repeatCharSet.Add(charArr[i]);
                }
                else
                {
                    charSet.Add(charArr[i]);
                }
            }

            List<char> destCharList = new List<char>();
            for (int i = 0; i < charArr.Length; ++i)
            {
                if (!repeatCharSet.Contains(charArr[i]))
                {
                    destCharList.Add(charArr[i]);
                }
            }
            char[] destCharArr = destCharList.ToArray();

            string destStr = new string(destCharArr);
            Console.WriteLine(destStr);
        }

追问

大哥 你这是c语言吗 怎么都看不懂

追答

额,你这#C。。。我还以为是C#呢,C语言的代码如下即可:

#include
#include
#include

#ifndef NULL
#define NULL (0)
#endif

//查找重复的字符,如果是重复的就返回0,不重复返回1
int check_repeat(char *dst_chars, int arr_size, char target)
{
int i = 0;
for (i = 0; i < arr_size; ++i) {
if ('\0' == dst_chars[i]) {
// 如果遇到字符串结束符,说明已经没有必要再找下去了
break;
}
if (dst_chars[i] == target) {
return 0;
}
}
return 1;
}

int main(int argc, char **argv)
{
int i = 0;
int j = 0;
int str_max_len = 0;
char teststr[] = "hello,world!";
char *not_repeat_chars = NULL;

str_max_len = strlen(teststr) + 1;
not_repeat_chars = (char *)malloc(sizeof(char) * str_max_len);
if (NULL == not_repeat_chars) {
printf("memory error\n");
return 1;
}
memset(not_repeat_chars, 0, str_max_len);

j = 0;
for (i = 0; i < str_max_len; ++i) {
if (0 != check_repeat(not_repeat_chars, str_max_len, teststr[i])) {
not_repeat_chars[j++] = teststr[i];
} else {
continue;
}
}
printf("%s\n", not_repeat_chars);

free(not_repeat_chars);
not_repeat_chars = NULL;
return 0;
}

本回答被网友采纳
第3个回答  2017-01-14
用数组变量,每次输入后全部遍历检测一遍
由于不熟悉c语言所以不可以提供源代码,如果需要可以百度一下
如果是作业请忽略本条消息,这样会被追着打的
相似回答