如何删除字符数组中的所有重复字符

时间:2021-04-29 15:45:07

标签: arrays c

我想从数组中删除所有重复的字符。这是示例。

"aabccdee"
"bd"

我正在做这个 C 语言。只使用数组、循环、if、else(条件语句),不使用指针。

#include<stdio.h>

int main() {
    char c[10];
    char com[10] = {0,};
    char result[10] = { 0, };
    int cnt = 0;
    for (int i = 0; i < 10; i++) {
        scanf("%c", &c[i]);
    }

    for (int i = 0; i < 10; i++) {
        for (int j = i+1; j < 10; j++) {
            if (c[i] == c[j]) {
                com[i] = c[i];
                cnt++;
                printf("%c", com[i]);
            }
        }
    }

    for (int i = 0; i < cnt; i++) {
        for (int j = 0; j < 10; j++) {
            if (com[i] != c[j]) {
                result[j] = c[j];
            }
        }
    }
    printf("\n");
    for (int i = 0; i < 10; i++) {
        printf("%c", result[i]);
    }
}

我是这么想的

  1. 制作重复数组
  2. 比较原始数组和重复数组
  3. 输出

但是重复数组循环不能循环所有原始数组。 如何删除所有重复的字符?

2 个答案:

答案 0 :(得分:0)

首先在我们说话之前,你必须检查this

使用 scanf 扫描字符时需要放置一个空格 所以

        scanf("%c", &c[i]);

变成

        scanf(" %c", &c[i]);

其次,您的想法有点混乱,因为结果表明您只是在处理案例,而不会继续验证整个数组。你需要学习how to shift an array to the right or left

稍后你的问题是当你移动你的桌子(不完全)时,你仍然打印出尺寸。

所以基本上你的代码应该是这样的:

#include<stdio.h>
int main() {
  char c[10];
  int length=5;
   for (int i = 0; i < 5; i++) {
       scanf(" %c", &c[i]);
   }
   int j,k,i;
   for(i=0; i<length; i++)
  {
      for(j=i+1; j<length; j++)
      {
          if(c[i] == c[j])
          {
                length--;
                for(k=j; k<length; k++)
                {
                   c[k] = c[k + 1];
                }
            j--;
        }
    }
  }
    printf("\n");
    for (int i = 0; i < length; i++) {
        printf("%c", c[i]);
    }
}
   

您只需选取一个案例并将其与其他案例进行比较,如果它存在,则您第二次从元素找到的位置移动,依此类推

答案 1 :(得分:0)

公然回答家庭作业不是很好的 SO 政策,但我很少这样做,并认为这是一项有趣的任务。当然没有对效率提出任何要求,但它看起来对我有用。据我所知,第一个和最后一个案例是极端案例,所以我单独处理它们,并在中间的所有内容中使用循环。如果您不被允许使用 strlen,那么您可以自己滚动或使用其他方法,这不是这个问题的主要焦点(最好从命令行 fgets 字符串论据)。

#include <stdio.h>
#include <string.h>

int main(void)
{
    char source[] = "aabccdee";
    char result[sizeof(source)] = { 0 };

    unsigned resultIndex = 0;
    unsigned i = 0;

    // do this to avoid accessing out of bounds of source.
    if (strlen(source) > 1)
    {
        // handle the first case, compare index 0 to index 1. If they're unequal, save
        // index 0.
        if (source[i] != source[i+1])
        {
            result[resultIndex++] = source[i];
        }

        // source[0] has already been checked, increment i to 1.
        i++;
    
        // comparing to strlen(source) - 1 because in this loop we are comparing the
        // previous and next characters to the current. Looping from 1 to second-to-the-
        // last char means we stay in bounds of source
        for ( ; i < strlen(source) - 1; i++)
        {
            if (source[i-1] != source[i] && source[i] != source[i+1])
            {
                // write to result if curr char != prev char AND curr char != next char
                result[resultIndex++] = source[i];
            }
        }
    
        // handle the end. At this point, i == the last index of the string. Compare to
        // previous character. If they're not equal, save the last character.
        // 
        if (source[i] != source[i-1])
        {
            result[resultIndex] = source[i];
        }
    }
    else if (strlen(source) == 1)
    {
        // if source is only 1 character, then it's trivial
        result[resultIndex] = source[i];
    }
    else
    {
        // source has no length
        fprintf(stderr, "source has no length.\n");
        return -1;
    }

    // print source and result
    printf("source = %s\n", source);
    printf("result = %s\n", result);

    return 0;
}

source 的各种输出:

source = "aabccdee"
result = "bd"

source = "aaee"
result = 

source = "a"
result = "a"

source = "abcde"
result = "abcde"

source = "abcdee"
result = "abcd"

source = "aabcde"
result = "bcde"

source = "aaaaaaaaaaaabdeeeeeeee"
result = "bd"

source = ""
source has no length.
相关问题