Exercise 1
Counting Characters in a File
Due Date - January 20, 2012 @ 2pm
Introduction
Often, the inputs that programs need will come from files. It is therefore important to know how to open and access files in C; in fact, managing files is one of the three outcomes of ECE 264. This exercise will introduce you to the various functions in C that accomplish these tasks.
Assignment
Write a program that reads a file and prints out the number of times different alphabetic characters appears in the file (disregarding case, i.e. 'a' is the same as 'A'), as well as the total number of characters in the file.
Output Code
You can download the code for the expected output here.
Specifications
-
The name of file to be examined will be given as a (single) command-line argument.
-
Upper and lower case forms of the characters should be treated the same.
-
Non-alphabetic characters (i.e. outside of the range A to Z and a to z) should be ignored.
Submission Procedure
Create a zip file containing the following files
To create a zip file in Linux terminal, type
> zip ex1.zip ex1.c
(> is the terminal prompt. Do not type it.)
and then submit the zip file (i.e. ex1.zip) to the course grading website.
Tips and Hints
We will be discussing how to solve this exercise during the lectures. However, as a hint, some of the file operation functions you will be needing to solve this problem include fopen, fclose, and fgetc.
Example
Let's say an example file called example1.txt that contains the pharse "There is no place like home". The resulting output should be as followed
> ex1 example1.txt
a 1
b 0
c 1
d 0
e 5
f 0
g 0
h 2
i 2
j 0
k 1
l 2
m 1
n 1
o 2
p 1
q 0
r 1
s 1
t 1
u 0
v 0
w 0
x 0
y 0
z 0
Total count - 22
|