Alex Alex - 1 month ago 9
C Question

counting occurrence of character and printing them out

I've managed to get the frequency of the alphabet from my input. I also found the largest occurred character. But i dont know how to print the actual character. Right now my program displays A-Z and display the occurrence for each alphabet. I want to be able on the next line print out the largest occurred letter and how many times it occurred.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 200

int readLine(char string[]);
void find_frequency(char string[], int count[]);
int maxArray(int a[]);

int main(void)
{
char array[MAX];
int freq[MAX];
int nrOfChar;
int i;
char c;
int max;

printf("Command Line Tool\n");
printf("Please enter text here: ");
nrOfChar = readLine(array);

for(c = 'A'; c<= 'Z'; c++)
{
printf("%c ", c);
}


find_frequency(array, freq);

printf("\n");
for(i=0;i<26;i++)
{
printf("%d ", freq[i]);
}
printf("\n");

max=maxArray(freq);

printf("Print letter and how many occurrence.\n");
printf("Finished excuting.\n");

return 0;
}

int readLine(char string[])
{
int ch;
int i=0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF)
{
if (i < MAX)
{
string[i++] = ch;
ch = getchar();
}
}
string[i] = '\0';
return i;
}

void find_frequency(char string[], int count[])
{
int i;

for(i = 0; string[i] != '\0'; i++)
{
if (string[i] >= 'A' && string[i] <= 'Z' )
{
count[string[i]-'A']++;
}
}
}

int maxArray(int a[])
{
int i, max=0;

for (i=0; i<26; i++)
{
if (a[i]>max)
{
max=a[i];
}
}


return max;
}

Answer

Below I've incorporated various folks' suggestions from the comments as well as some other fixes and style changes for you to consider:

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

#define ALPHABET_LENGTH 26
#define MAXIMUM_CHARACTERS 200

int readLine(char string[], int maximum);
void findFrequency(char string[], int count[]);
int findMaximumIndex(int array[], int length);

int main(void)
{
    char characters[MAXIMUM_CHARACTERS];
    int frequencies[ALPHABET_LENGTH];

    printf("Command Line Tool\n");
    printf("Please enter text here: ");
    (void) readLine(characters, MAXIMUM_CHARACTERS);

    for (char c = 'A'; c <= 'Z'; c++)
    {
        printf("%c ", c);
    }
    printf("\n");

    findFrequency(characters, frequencies);

    for (int i = 0; i < ALPHABET_LENGTH; i++)
    {
        printf("%d ", frequencies[i]);
    }
    printf("\n");

    int maximum = findMaximumIndex(frequencies, ALPHABET_LENGTH);

    printf("%c occurred most often (%d times)\n", maximum + 'A', frequencies[maximum]);
    printf("Finished excuting.\n");

    return 0;
}

int readLine(char string[], int maximum)
{
    int c, count = 0;

    while (isspace(c = getchar()))
        ;

    while (c != EOF && c != '\n')
    {
        if (count < maximum - 1)
        {
            string[count++] = c;
            c = getchar();
        }
    }

    string[count] = '\0';

    return count;
}

void findFrequency(char string[], int count[])
{
    for (int i = 0; string[i] != '\0'; i++)
    {
        char c = string[i];

        if (c >= 'A' && c <= 'Z' )
        {
            count[c - 'A']++;
        }
    }
}

int findMaximumIndex(int array[], int length)
{
    int index = 0;

    for (int i = 1; i < length; i++)
    {
        if (array[i] > array[index])
        {
            index = i;
        }
    }

    return index;
}

EXAMPLE OUTPUT

> ./a.out
Command Line Tool
Please enter text here: IN TEACHING OTHERS WE TEACH OURSELVES
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
2 0 2 0 6 0 1 3 2 0 0 1 0 2 2 0 0 2 3 3 1 1 1 0 0 0 
'E' occurred most often (6 times)
Finished excuting.
> 

Things yet to ponder: your output won't look as nice if any character occurs more than 9 times -- how can you fix that; you might want to deal with mixed case as an option in your readLine() routine to uppercase the input; there may be a (multi-way) tie for the most frequently occuring letter, can you deal with that; your readLine() returns a count of characters that you don't do anything with -- how might it be useful (possibly in error checking)?

Comments