-4

I am trying to make my histogram print only a to z and not spaces or other symbols. From my code below how could I achieve this. I have set c to ignore case but i cannot figure out how to ignore white spaces and etc.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define READ 5000

main() {
    int c;
    int i;
    int x;
    int length[READ];
    {
        for (x = 0; x < READ; x++)
            length[x] = 0;
        while ((c = getchar()) != EOF) {
            c = tolower(c); // ignores all cases.
            length[c]++;
            if (c == EOF)
                break;
        }
    }
    for (x = 0; x < READ; x++) {
        if (length[x] > 0) {
            printf("%c| ", x);
            for (i = 1; i <= length[x] / 5; ++i) {
                // since the value i is int when a value less than 5
                // such as 4/5 appears as   0. 5/5 to 9/5 is 1 "x"
                printf("X"); // prints an X for every 5 of a character
            }
            printf("\b\b\b\b\b\t\t\t(%d)", length[x]);
            printf("\n"); // aligns the histogram with new line
        }
    }
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
Herb
  • 11
  • 4

2 Answers2

0

Not 100% sure what you are looking for, but perhaps:

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

  int main ( void ) {
     int c;
     int i;
     int x;
     int n;  
     int count[26];
     int N = (sizeof(count)/sizeof(count[0]));

     for(x = 0; x < N; x++) count[x] = 0;
     while ((c = getchar() ) != EOF) {
        if (!isalpha(c)) continue;
        i = tolower(c) - 'a';
        count[i]++;
     }
     for (x = 0; x < N; x++) {
        if (count[x] == 0) continue;
        printf("%c| ", x + 'a');
        n = count[x]/5;
        for(i = 1; i <= n; ++i) printf("X");
        printf(" %*s(%d)\n", 50 - n, "", count[x]);
     }
     return 0;
}

NOTE: most probably will not work on other than US ascii.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • 1
    To be more general, use `count[256]`, and omit the `- 'a'` and `+ 'a'` parts. – John Hascall Sep 11 '14 at 19:19
  • "NOTE: most probably will not work on other than US ascii." So, don't perpetuate the _myth that ASCII is being used_. It is reasonable to assume that a `char` is 8 bits and therefore there are 256 `char` values. – Tom Blodget Sep 11 '14 at 23:08
0

The isalpha() is the best place to start to filter out non-alpha char.

Use #define READ (UCHAR_MAX + 1)

   while ((c = getchar()) != EOF) {
     if (isalpha(c) {
       c = tolower(c);
       length[c]++;
     }
   }

To deal with printing the histogram, use the return value from printf() as it reports the number of char printed to calcualte the offset. Use "%*s" to print the needed spacer.

for (x = 0; x < READ; x++) {
  int maxoffset = 0;
  if (length[x] > 0) {
    char buffer[100];
    int offset = sprintf(buffer, "%c| ", x);
    for (i = 1; i <= length[x] / 5; ++i) {
      offset += printf("X"); // prints an X for every 5 of a character
    }
    // Print length on the 40th column
    offset = 40 - offset;
    if (offset <= 0) offset = 1;
    printf("%*s(%d)\n", offset, "", length[x]);
  }
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256