samir82show

c_ch01_ex14.c

Sep 26th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. /*Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.*/
  2. #include <stdio.h>
  3. #define INDEX 27
  4. int main()
  5. {
  6. int histog[INDEX], c;
  7. for (c = 0;c < INDEX; c++)
  8. histog[c] = 0;
  9. while ((c = getchar()) != EOF) {
  10. switch (c) {
  11. case 'a': ++histog[0];
  12. break;
  13. case 'b': ++histog[1];
  14. break;
  15. case 'c': ++histog[2];
  16. break;
  17. case 'd': ++histog[3];
  18. break;
  19. case 'e': ++histog[4];
  20. break;
  21. default : ++histog[26];
  22. }
  23. }
  24. printf ("char histogram are histog[a] = %d\n", histog[0]);
  25. printf ("char histogram are histog[b] = %d\n", histog[1]);
  26. printf ("char histogram are histog[c] = %d\n", histog[2]);
  27. printf ("char histogram are histog[d] = %d\n", histog[3]);
  28. printf ("char histogram are histog[e] = %d\n", histog[4]);
  29. printf ("char histogram are histog[other] = %d\n", histog[26]);
  30. return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment