Return 按用户输入 C 顺序排列的 ASCII 字符数

Return count of ASCII characters in order of user input C

我需要 return 按 ASCII 字符在用户输入接收到的字符数组中出现的顺序计数

我目前的解决方案是 return按字符在 ASCII 中出现的升序 table 而不是按用户输入的顺序

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

int main()
{
  char string[16];
  int c = 0, count[128] = {0}, x, counted = 0;

  printf("Enter a word>\n");
  scanf("%s", string);

  while (string[c] != '[=11=]') {
    if(string[c] >= '!' && string[c] <= '~'){
      x = string[c] - '!';
      count[x]++;
    }
    c++;
  }

  for (c = 0; c < 128; c++){
    if(count[c] > 1){
    printf("Duplicate letter: %c, Occurrences: %d\n", c + '!', count[c]);
      counted++;
    }
  }

  if(counted < 1){
    printf("No duplicates found\n");
  }
  return 0;
}

示例输入:

AAAAaaaaBBBbb99

期望输出:

重复字母:A,出现次数:4
重复字母:a,出现次数:4
重复字母:B,出现次数:3
字母重复:b,出现次数:2
重复字母:9,出现次数:2

我当前(错误)的输出:

重复字母:9,出现次数:2
重复字母:A,出现次数:4
重复字母:B,出现次数:3
重复字母:a,出现次数:4
字母重复:b,出现次数:2


非常感谢这里的任何帮助

不是一个非常优雅的解决方案,但它有效:

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

int main() {
    char string[1024];
    int c = 0;
    int count[128] = {0};
    int x;
    int counted = 0;

    printf("Enter a word:\n");
    scanf("%1023s", string);

    while (string[c] != '[=10=]') {
        if(string[c] >= '!' && string[c] <= '~'){
            x = string[c] - '!';
            count[x]++;
        }
        c++;
    }

    int j = 0;
    while (string[j] != '[=10=]') {
        int ch = string[j] - '!';

        if(count[ch] > 1){
            printf("Duplicate letter: %c, Occurrences: %d\n", ch + '!', count[ch]);
            count[ch] = -1;
            counted++;
        }

        j++;
    }

    if(counted < 1){
        printf("No duplicates found.\n");
    }

    return 0;
}