cf.c 521 B

123456789101112131415161718192021222324252627282930313233
  1. /* cf - print character frequencies */
  2. float f[128];
  3. main(argc, argv)
  4. int argc;
  5. char *argv[];
  6. {
  7. int i, c, nc;
  8. float cutoff, atof();
  9. if (argc <= 1)
  10. cutoff = 0.0;
  11. else
  12. cutoff = atof(argv[1])/100;
  13. for (i = 0; i <= 127; )
  14. f[i++] = 0.0;
  15. nc = 0;
  16. while ((c = getchar()) != -1) {
  17. f[c] += 1;
  18. nc++;
  19. }
  20. printf("char\tfreq\n");
  21. for (i = 0; i <= 127; ++i)
  22. if (f[i] && f[i]/nc >= cutoff) {
  23. if (i <= ' ')
  24. printf("%03o", i);
  25. else
  26. printf("%c", i);
  27. printf("\t%.1f\n", 100*f[i]/nc);
  28. }
  29. return 0;
  30. }