【问题标题】:Count how many of each and every char there is in an array计算数组中每个字符的数量
【发布时间】:2016-12-01 16:52:31
【问题描述】:

我试图显示用户选择的句子中的哪个字符以及每个字符有多少。因此,如果用户输入“Hello World!”程序应返回一个字符及其使用次数。

" SPACE:1, !:1, H:1, W:1, e:1, d:1, l:3, o:2, r:1,"

我将它放在一个开关中,因为我有其他选择供用户选择。

现在我可以找出使用的字符以及从 SPACE 到 Q 有多少个。我也可以找出所有的小写字母,但如果它读到“a”,它会说有 1 个“a” ' 和一个空格(在 ASCII 码中,它从 32 开始,随着小写字母的上升而上升)。

这些是我使用的变量。

int menyval = 0, i, k = 0, h, j, count, count2;
char input, str[100], getridof, add, character;

这是我在这种情况下所拥有的。

printf("Write a string not more then 50 chars:\n");
        getchar();
        i = 0;
        j = 0;
        count = 0;
        int counts[50] = { 0 };
        gets(str);
        str[j] = str[i];
            while (str[i] != '\0') {


                if (str[i] >= 97 && str[i] <= 122) {
                    counts[str[i] - 97]++;
                }
                i++;
                count++;
            }

            for (i = 0; i < 50; i++) {
                if (counts[i] != 0) {
                    printf("%c: %d\n", i + 97, counts[i]);
                }
            }

            while (str[j] != '\0') {


                if (((str[j] >= 32 && str[j] <=96)) || ((str[j] >=123 && str[j] <= 126))) {
                    counts[str[j] - 32]++;
                }
                j++;
            }

            for (j = 0; j < 50; j++) {
                if (counts[j] != 0 ) {
                    //if((j) < 127)
                    printf("%c: %d\n", j + 32, counts[j]);
                }
            }
        printf("Total amount of char: %d\n", count);
        str[i] = '\0';
        system("pause");
        system("cls");

这是一项学校作业,所以如果您不想说出直接代码,我理解,但我会非常感谢您提供一些提示,为我指明正确的方向。

【问题讨论】:

  • 您的counts 数组不够大,无法容纳您尝试在j 的循环中存储的值的数量。数组中只有50 整数槽,但您的索引远远超出此范围(96-32=64)。您也不会在打印它和尝试再次使用它之间重新归零您的数组。我假设您确实想这样做。
  • 使用地图可以实现更简洁的实现。请查看地图文档。
  • BTW str[i] = '\0'; 写入越界。

标签: c arrays counter


【解决方案1】:

我很少用这种方式纠正和清除你自己的代码:

  1. 删除未使用变量的声明
  2. 将所有声明放在顶部
  3. 删除无用的命令。
  4. “magic” 数字更改为 6597 符号 'A''a' - 是的,chars 数字。
  5. cmets 用于代码的各个部分。
  6. 还有——当然——更正错误,主要是:
    1. 重置计数器
    2. 不连续符号范围(原始条件为||)分割成2个连续

所以现在完整的代码是:

#include <stdio.h>

int main() {
    int  i, count = 0, counts[50] = { 0 };
    char str[100];

    printf("Write a string not more than 50 chars:\n");
    gets(str);

    /* Counting capital letters and all symbols, too*/
    i = 0;
    while (str[i] != '\0') {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            counts[str[i] - 'A']++;
        }
        i++;
        count++;
    }

    /* ... and printing results */
    for (i = 0; i < 50; i++) {
        if (counts[i] != 0) {
            printf("%c: %d\n", i + 'A', counts[i]);
        }
    }

    /* ... and clear the counter */
    for (i = 0; i < 50; i++)
        counts[i] = 0;

    /* Counting small letters */
    i = 0;
    while (str[i] != '\0') {
        if (str[i] >= 'a' && str[i] <= 'z') {
            counts[str[i] - 'a']++;
        }
        i++;
     }

    /* ... and printing results */
    for (i = 0; i < 50; i++) {
        if (counts[i] != 0) {
            printf("%c: %d\n", i + 'a', counts[i]);
        }
    }

    /* ... and clear the counter */
    for (i = 0; i < 50; i++)
        counts[i] = 0;

    /* Counting symbols between SPACE and 'A' */
    i = 0;
    while (str[i] != '\0') {
         if ((str[i] >= ' ' && str[i] < 'A')) {
            counts[str[i] - ' ']++;
        }
        i++;
    }

    /* ... and printing results */
    for (i = 0; i < 50; i++) {
        if (counts[i] != 0 ) {
            printf("%c: %d\n", i + ' ', counts[i]);
        }
    }

    /* ... and clear the counter */
    for (i = 0; i < 50; i++)
        counts[i] = 0;

    /* Counting symbols over 'z' */
    i = 0;
    while (str[i] != '\0') {
         if ((str[i] >= 123 && str[i] <= 126)) {
            counts[str[i] - 123]++;
        }
        i++;
    }

    /* ... and printing results */
    for (i = 0; i < 50; i++) {
        if (counts[i] != 0 ) {
            //if((i) < 127)
            printf("%c: %d\n", i + 123, counts[i]);
        }
    }


    printf("Total amount of char: %d\n", count);
    str[i] = '\0';
    system("pause");
    system("cls");
    return 0;
}

我对其进行了测试,现在它可以正常工作了——尽管它仍然很丑。但它主要是你的代码,所以你会理解它

【讨论】:

  • 不要使用gets。并删除str[i] = '\0';
【解决方案2】:

ACII 表:http://www.asciitable.com/

    char str[12] = "hello world";

    // initialize an array of each possible character
    int charCount[128];
    memset(charCount, 0, sizeof(charCount));

    // iterate through the array of characters
    // incrementing the index in charCount matching the element in str
    char* currChar = str;
    while(*currChar)
            ++charCount[*(currChar++)];

    // iterate through the array once more
    for(int i = 0; i < 128; ++i) {
            // if the character was found in the string,
            // print it and its count
            if(charCount[i]) {
                    printf("%c: %d\n",i,charCount[i]);
            }
    }

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2019-09-30
    • 2015-08-25
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2018-05-13
    相关资源
    最近更新 更多