简体   繁体   English

计算数组中每个字符的数量

[英]Count how many of each and every char there is in an array

I'm trying to show which character and how many of each of them that are in an sentence that the user choose. 我试图显示用户选择的句子中的哪个字符以及每个字符中有多少个。 So if the user puts in "Hello World!" 因此,如果用户输入“ Hello World!” the program should give back one char and the number of times it is used. 程序应返回一个字符和使用次数。

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

I'm having it in an switch because I have other choices that the user can choose between. 之所以选择它,是因为我还有其他选择可供用户选择。

Right now I can get out what char that is used and how many of them from SPACE to Q. I also can get out all the small letters but if it reads an 'a' it will say that there is 1 'a' and one SPACE (in ASCII code it start over from 32 and goes up as the small letters goes up). 现在,我可以找出所使用的字符以及从SPACE到Q的字符。我还可以找出所有小写字母,但是如果它读为'a',则会说有1个'a'和一个SPACE(在ASCII代码中,它从32开始并且随着小写字母的增加而增加)。

These are the variables that I use. 这些是我使用的变量。

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

Here is what I have in this case. 这是我在这种情况下所拥有的。

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");

This is a school assignment, so I understand if you don't want to say the direct code but I'll be very thankfull for some hints to point me in the right direction. 这是学校的任务,所以我明白您是否不想说出直接代码,但我会非常感谢您为我提供了正确的指导。

ACII table: http://www.asciitable.com/ 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]);
            }
    }

I little corrected and cleared your own code in this ways: 我很少用这种方式纠正和清除您自己的代码:

  1. Deleting declaration of unused variables . 删除未使用变量的声明。
  2. Putting all declaration at top . 将所有声明放在顶部
  3. Deleting useless commands. 删除无用的命令。
  4. Changing "magic" numbers as 65 and 97 to the symbols 'A' , 'a' - yes, char s are numbers. “魔术”数字分别从6597更改为符号'A''a' a'-是的, char 数字。
  5. Putting comments for individual parts of your code. 为代码的各个部分添加注释
  6. And - of course - correcting errors, mainly: 而且-当然-纠正错误,主要是:
    1. reseting counters , 重置计数器
    2. splitting discontinuous range of symbols ( || in your original condition) into 2 continuous . 将符号的不连续范围(在您的原始状态下|| )分为2个连续的

So the full code is now: 因此,完整的代码现在是:

#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;
}

I tested it and now it works OK - in spite of it is still ugly. 我测试了它,现在它可以正常工作-尽管它仍然很丑陋。 But it is dominantly your code so you will understand it . 但这主要是您的代码,因此您将理解它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM