Thread: Count number of occurrences of each character in input

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Count number of occurrences of each character in input

    I want to count the number of occurrences of each character found in the input and print it to the output.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char **argv)
    {
        unsigned int countChars = 0, i = 0, k = 0;
        char c, s[100], str[100];
        str[k] = '\0';
        while((c = fgetc(stdin)) != EOF)
        {
            s[i++] = c;
            countChars++;
        }
    //    printf("%s\n", s);
        for(int i = 0; i < strlen(s); i++)
        {
            unsigned int countOccurrences = 1;
            for(int j = i+1; j <= strlen(s); j++)
            {
                if(s[i]==s[j])
                {
                    countOccurrences++;
                }
                    /*if(strchr(str, s[i]) == NULL)
                    {
                        str[k] = s[i];
                        k++;
                    }
                }
                for(k = 0; k < strlen(str); k++)
                {
                    fprintf(stdout, "The character %c has %u occurrences.\n", str[k], countOccurrences);
                }*/
            }
            fprintf(stdout, "The character %c has %u occurrences.\n", s[i], countOccurrences);
        }
    
    
        return 0;
    }
    So far for the input:
    john eats apples
    Output
    The character j has 1 occurrences.
    The character o has 1 occurrences.
    The character h has 1 occurrences.
    The character n has 1 occurrences.
    The character has 2 occurrences.
    The character e has 2 occurrences.
    The character a has 2 occurrences.
    The character t has 1 occurrences.
    The character s has 2 occurrences.
    The character has 1 occurrences.
    The character a has 1 occurrences.
    The character p has 2 occurrences.
    The character p has 1 occurrences.
    The character l has 1 occurrences.
    The character e has 1 occurrences.
    The character s has 1 occurrences.
    The character
    has 1 occurrences.
    The character  has 1 occurrences.

    So I need to add the characters in an array an check that each character appears only once in the array. I tried doing that with strchr and but only garbage was printed. Any ideas?

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    This isn't the only problem, but fgetc() returns int (not char), so c also needs to be declared as int.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Okay, so if I declare c as an int could implement the same logic and just typecast c to char?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    I think you need to learn how to enable warnings in your code flags. Your code has quite a few errors.

    clang -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99 -fsanitize=address,undefined -c main.c
    main.c:15:13: error: declaration shadows a local variable [-Werror,-Wshadow]
    for(int i = 0; i < strlen(s); i++)
    ^
    main.c:6:34: note: previous declaration is here
    unsigned int countChars = 0, i = 0, k = 0;
    ^
    main.c:15:22: error: comparison of integers of different signs: 'int' and 'unsigned long long' [-Werror,-Wsign-compare]
    for(int i = 0; i < strlen(s); i++)
    ~ ^ ~~~~~~~~~
    main.c:18:28: error: comparison of integers of different signs: 'int' and 'unsigned long long' [-Werror,-Wsign-compare]
    for(int j = i+1; j <= strlen(s); j++)
    ~ ^ ~~~~~~~~~
    3 errors generated.

  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    6
    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    void countOccurrences(char* str)
    {
        // 1) Count occurrences.
        int occurrences[256] = {0};
        for(; *str; str++)
            occurrences[(unsigned char) *str]++;
    
    
        // 2) Print occurences.
        for(size_t i = 0; i < 256; i++)
            if(occurrences[i])
                printf("'%c' occurs %d times\n", (char) i, occurrences[i]);
    }
    
    int main()
    {
        countOccurrences("Hello World");
        return 0;
    }
    Code:
    ' ' occurs 1 times
    'H' occurs 1 times
    'W' occurs 1 times
    'd' occurs 1 times
    'e' occurs 1 times
    'l' occurs 3 times
    'o' occurs 2 times
    'r' occurs 1 times
    This counts and prints the unique occurrences of each char in str, which is probably what you wanted? Notice that the entire string needs to be in memory for this to work, which is more than is strictly necessary for your situation. Another implementation would be:

    Code:
    int main()
    {
        // 1) Count occurrences.
        int c, occurrences[256] = {0};
        while((c = fgetc(stdin)) != EOF)
            occurrences[(unsigned char) c]++;
        
        // 2) Print occurrences.
        for(size_t i = 0; i < 256; i++)
            if(occurrences[i])
                printf("'%c' occurs %d times\n", (char) i, occurrences[i]);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete all occurrences of a character
    By tinchi in forum C Programming
    Replies: 5
    Last Post: 05-17-2014, 09:48 PM
  2. Replies: 7
    Last Post: 12-04-2011, 10:43 PM
  3. Remove all occurrences of character from string
    By Scotty33 in forum C Programming
    Replies: 14
    Last Post: 09-27-2011, 04:32 AM
  4. counts the number of occurrences of the phrase
    By munna_dude in forum C Programming
    Replies: 9
    Last Post: 06-16-2010, 12:16 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

Tags for this Thread