Home
Netbeans Eclipse Qt Java
Games
College of Engineering Aeronautics and Astronautics Agricultural and Biological Engineering Biomedical Engineering Chemical Engineering Civil Engineering Construction Engineering and Management Electrical and Computer Engineering Engineering Education Engineering Professional Education Environmental and Ecological Engineering Industrial Engineering Materials Engineering Mechanical Engineering Nuclear Engineering
EPICS (Engineering Projects In Community Service) First-Year Engineering Program First-Year Engineering Honors Program Global Engineering Program Minority Engineering Program Professional Practice (Co-Op) Program Women in Engineering Program
College Administration Schools Programs All Groups All People ECN Webmail
Purdue Home

Exercise 2

Counting Characters in a File

Due Date - September 4, 2012 @ 6pm

Introduction

Files in C      

A common way for programs to receive the inputs they need for execution is to read the inputs from files.  This exercise will introduce you to the various functions in C that allow the programmer to open, access, and close files.

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.  You will also write a Makefile that can be used to compile the multiple files that you will use to write your program.

Skeleton

You can download the skeleton for ex2 here.  It contains the following file

  • ex2.c - Contains the main function for this assignment
  • utility.c - Contains the auxillary functions for this assignment
  • utility.h - Contains all function headers and constants
  • Makefile - Contains sections that will compile, run some of the provided testcases, and compare your solution with the expected solution.  To run the part that will run your code with the provided testcases and do the comparison, type make test.

Do not modify the main function (do not modify the arguments and do not modify the function body). Do not modift the PrintError function. Do not add new files. Do not remove files (your zip file must contain ex2.c, utility.c, utility.h, and Makefile). Do not modify the names, return types, and arguments of the functions in the skeleton. If you do so, your program may fail the computer grader. 

For this exercise, you should change only utility.c.

The computer grader will replace the main function by a set of different main functions (called main1.c, main2.c, main3.c...) for testing different parts of your program.

You can (and should) add or change the code inside the bodies of the functions. You can add additional functions inside the file utility.c.

Specifications

  1. The name of file to be examined will be given as a (single) command-line argument.
  2. Error message should be printed if the file to be examined is not given as a command-line argument or if the file given cannot be opened
  3. Upper and lower case forms of alaphabetic characters should be counted together.  
  4. Non-alphabetic characters (numbers, punctuation marks, etc.) should be ignored.
  5. If the program executes successfully, your program at the end should return the value EXIT_SUCCESS.  If a problem occurs (such as the file not opening), you should return the value EXIT_FAILURE.  EXIT_SUCCESS and EXIT_FAILURE are constants defined in the stdlib.h library
  6. Your code should follow the C coding standards expressed here.  Faillure to abide these standard will result in loss of points.You can find a guide with good programming practices here.
  7. Your code should be commented to describe the functions and any important algorithm sections.  Doing this make reading your code easier for the graders.  

Testcases

The skeleton zip file contains a set of testcases. Type "make test" to run some test cases. The computer grader is not ready right now (sorry!). We will make the computer grader ready as soon as possible.

Submission Procedure

Create a zip file containing the following files

  • ex2.c
  • utility.c
  • utility.h
  • Makefile

To create a zip file in Linux terminal, type

zip ex2.zip ex2.c utility.c utility.h Makefile

and then submit the zip file (i.e. ex2.zip) to Blackboard.

You can also submit this .zip file to the course testing website for additional testcases. 

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.

Here is a link to a webpage containing the ASCII values for all characters -> ASCII table.

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

> ex2 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