Skip to content

Instantly share code, notes, and snippets.

@nobane
Last active December 27, 2015 08:29
Show Gist options
  • Save nobane/7297096 to your computer and use it in GitHub Desktop.
Save nobane/7297096 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
printf("Program: Array\nAuthor: Lacy Timlick\n");
char string[255];
fflush(stdout);
printf("Enter a string:\n");
gets(string);
int c = 0,
count[53] = {0}, // 26 lower case letters + 26 upper + " " = 53 possible character counts
string_length = strlen(string); // This variable can be declared and set on one line
printf("FREQUENCY TABLE\n");
printf("--------------------------------\n");
printf("Char\tCount\t%% of Total\n");
printf("----\t-----\t----------------\n");
printf("ALL\t%d\t100%%\n", string_length); // The "ALL" line appears to be just the total count of characters
while ( string[c] != '\0' )
{
// Study this link if you haven't studied ascii tables yet: http://web.cs.mun.ca/~michael/c/ascii-table.html
if (string[c] >= 'A' && string[c] <= 'Z') // A = 65 ... Z = 90
count[ string[c] - 'A' ]++; // By subtracing 'A', 'A' will be stored as count[0] and so on
else if (string[c] >= 'a' && string[c] <= 'z') // a = 97 ... Z = 122
count[ string[c] - 71 ]++; // By subtracting 71, 'a' will be stored as count[26] and so on
else if (string[c] == ' ')
count[52]++; // Designate count[52] to represent ' ' count
c++;
}
for ( c = 0 ; c < 53 ; c++)
{
if (count[c] != 0)
{
char character;
if (c <= 25)
character = c + 'A';
else if (c <= 51) // since this is 'else if' *after* we check for c <=25, it implies: c > 25 && c <= 51
character = c + 71;
else if (c == 52)
character = ' ';
printf("%c\t%d\t%f\n", character, count[c], (float)count[c] / string_length * 100);
}
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment