0

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!" 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).

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.

dirtydanee
  • 6,081
  • 2
  • 27
  • 43
  • 1
    Do not use `gets()`.http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – aeb-dev Dec 01 '16 at 16:58
  • Your `counts` array is not large enough to hold the number of values you're trying to store in the loop with `j`. You only have `50` integer slots in the array, but you're indexing well beyond that (`96-32=64`). You're also not re-zeroing your array between printing it and trying to use it again. I assume you actually want to do that. – eddiem Dec 01 '16 at 17:05
  • A cleaner implementation can be done using a map. Please check the map documentation. – Ehsan Dec 01 '16 at 17:05
  • BTW `str[i] = '\0';` write to out of bounds. – BLUEPIXY Dec 01 '16 at 17:07

2 Answers2

1

ACII table: 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]);
            }
    }
ibstevieb123
  • 96
  • 10
1

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, chars are numbers.
  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.

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.

MarianD
  • 13,096
  • 12
  • 42
  • 54