Conditionals and Iteration
9. Example::Histogram

Write a C program that computes a histogram of character occurrences in a given text and prints character values and occurrences alphabetically. A character is included in the output only if it occurs at least once in the text.

Notes:

  • The input text contains English characters and white spaces.
  • The length of the input text does not exceed 100 characters.
  • The program is case insensitive; e.g., 'a' is equal to 'A'.
  • You may use the fgets() function to read a line from the standard input. Note that gets() is dangerous and its usage is discouraged.
Here is a sample code that demonstrate the usage of fgets().

Sample Run:

---Input---
introduction TO computer SCIENCE
---Output---
C:4
D:1
E:3
I:3
M:1
N:3
O:4
P:1
R:2
S:1
T:4
U:2

---Input---
Write a C program that computes a histogram of character occurrences
---Output---
A:7
C:7
E:5
F:1
G:2
H:3
I:2
M:3
N:1
O:5
P:2
R:8
S:3
T:6
U:2
W:1

Difficulty: 5 / 10
Home | About | Contact | Feedback