Thursday, November 29, 2018

C code review Histogram of Frequency of Different Characters in Input

This C code shows Histogram of Frequency of Different Characters in Input. And code is helpful while user is in between string manipulation task.

code:

//Tested Ok in Code::Blocks IDE 17.12.
//Logic works in Cygwin byt Ctrl^z operation makes problem.

/* Histogram of Frequency of Different Characters in Input */

#include<stdio.h>

#define TNOCHAR 128  /* Total Number of characters is 128: 0 - 127 */

int main(void)
{
    int c,i,j;

    int character[TNOCHAR];

    printf("Type the string to check each characters frequency histogram: \n\t");
    printf("Press EnterKey+Ctrl^Z to terminate it.\n");

    for(i=0; i<TNOCHAR; ++i)
        character[i] = 0;

    while((c=getchar())!=EOF)
        ++character[c];

    for(i=0; i<TNOCHAR; ++i)
    {
        putchar(i);

        for(j=0; j<character[i]; ++j)
            putchar('*');

        putchar(' ');
    }
    putchar('\n');
    return 0;
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment