Khulna University, Khulna
Computer Science and Engineering Discipline
Course No: CSE 2102
Data Structures Laboratory
Problem Set: Files
1. Write a program that converts all letters in a file to upper case. (Characters
other than letters shouldn’t be changed.) The program should write its output
to stdout.
2. Write a program named that “concatenates” two files by writing them to standard
output, one after the other, with no break between files. Your program should
issue an error message if any file can’t be opened.
3. Write a program that counts
a) the number of characters in a text file.
b) the number of words in a text file, (A “word” is any sequence of non-white-
space characters.)
c) the number of lines in a text file.
4. Of the many techniques for compressing the contents of a file, one of the simplest
and fastest is known as run-length encoding, This technique compresses a file by
replacing sequences of identical characters by a pair: a repetition count followed
by a character to be repeated. For example, suppose that the file to be compressed
begins with the following sequence of characters:
LLLLGOGERYCHWYRNDROBWLLLLANTYSIIOGGGOCH
The compressed file will contain the following:
4L1G1O1G1E1R1Y1C1H1W1Y1R1N1D1R1O1B1W4L1A1N1T1Y1S2I1O3G1O1C1H
Run-length encoding works well if the original file contains many long sequences
of identical bytes. In the worst case (a tile with no repeated bytes) run-length
encoding can actually double the length of the file.
(a) Write a program named CompressFile that uses run-length encoding to com-
press a file.
(b) Write a program named UncompressFile that reverses the compression per-
formed by the CompressFile program.
5. The following table shows the daily flights from one city to another:
Departure time Arrival time
8:00 a.m. 10: 16 a.m.
9:43 a.m. 11:52 a.m.
11:19 a.m. 1:31 p.m.
12:47 p.m. 3:00 p.m.
2:00 p.m. 4:08 p.m.
3:45 p.m. 5:55 p.m.
7:00 p.m. 9:20 p.m.
9:45 p.m. 11:58 p.m.
Write a program that obtains departure and arrival times from a file named
[Link]. Each line of the file will contain a departure time followed by an
arrival time, with one or more spaces separating the two. Your program should
1
ask user to enter a time (expressed in hours and minutes, using the 24-hour clock).
The program then displays the departure and arrival times for the flight whose
departure time is closest to that entered by the user: Enter a 24-hour time:
13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
Hint: Convert the input into a time expressed in minutes since midnight, and
compare it to the departure times, also expressed in minutes since midnight. For
example, 13:15 is 13 × 60 + 15 = 795 minutes since midnight. which is closer
to 12:47 p.m, (767 minutes since midnight) than to any of the other departure
times.
6. One of the oldest known encryption techniques is the Caesar cipher, attributed to
Julius Caesar. It involves replacing each letter in a message with another letter
that is a fixed number of positions later in the alphabet. (If the replacement would
go past the letter Z the cipher “wraps around” to the beginning of the alphabet.
For example, if each letter is replaced by the letter two positions after it then Y
would be replaced by A and Z would be replaced by B.) Write a program that
encrypts a message using a Caesar cipher. The program would prompt the user
to enter the name of a tile containing the message to be encrypted and the shift
amount (the number of positions by which letters should be shifted:
Enter name of file to be encrypted: [Link]
Enter shift amount (1-25): 3
The program then writes the encrypted message to a tile with the same name but
an added extension of .enc. In this example, the original file name is [Link],
so the encrypted message will be stored in a file named [Link].
For example, message to be encrypted is Go ahead, make my day and shift is 3
then the encrypted message will be Jr dkhdg, pdnh pb gdb.
You may assume that the message does not exceed 80 characters. Characters
other than letters should be left unchanged. Lower-case letters remain lower-case
when encrypted, and upper-case letters remain upper-case. Hint: To handle the
wrap-around problem, use the expression ((ch−0 A0 )+n)%26+0 A0 to calculate the
encrypted version of an uppercase letter where ch stores the letter and n stores
the shift amount. (You’ll need a similar expression for lower-case letters.)
7. Write a program that reads a series of phone numbers from a file and displays
them in a standard format. Each line of the file will contain a single phone
number, but the numbers may be in a variety of formats. You may assume that
each line contains 10 digits, possibly mixed with other characters (which should
be ignored). For example, suppose that the file contains the following lines:
404.817.6900
(215) 686-1776
312-746-6000
877 275 5273
6173434200
The output of the program should have the following appearance:
(404) 817-6900
(215) 686-1776
(312) 746-6000
(877) 275-5273
(617) 343-4200
8. Write a program that reads integers from a text file. Each line of the file may
contain any number of integers (including none) separated by one or more spaces.
Have the program display the largest number in the file, the smallest number,
and the median (the number closest to the middle if the integers were sorted).
If the file contains an even number of integers. there will be two numbers in
the middle: the program should display their average (rounded down). You may
Page 2
assume that the file contains no more than 10000 integers.
Hint: Store the integers in an array and then sort the array.
9. Write a program that reads a student name followed by five test scores. The
program should output the student name, the five test scores, and the average
test score. Output the average test score with two decimal places. The data to
be read is stored in a file called [Link]. The output should be stored in a file
called [Link].
Input A file containing the student name and the five test scores. A sample
input is:
Sk Rayhan Islam 87.50 89 65.75 37 98.50
Output The student name, the five test scores, and the average of the five test
scores, saved to a file.
10. Three employees in a company are up for a special pay increase. You are given a
file, say [Link], with the following data:
Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1
Each input line consists of an employee’s last name, first name, current salary,
and percent pay increase. For example, in the first input line, the last name of
the employee is Miller, the first name is Andrew, the current salary is 65789.87,
and the pay increase is 5%. Write a program that reads data from the specified
file and stores the output in the file [Link]. For each employee, the data
must be output in the following form:
firstName lastName updatedSalary.
Format the output of decimal numbers to two decimal places.
11. Write a program that takes two arguments. The first is a character, and the sec-
ond is a filename. The program should print only those lines in the file containing
the given character.
12. Write a program that opens two files given by the user.
a. Have the program print line 1 of the first file, line 1 of the second file, line 2
of the first file, line 2 of the second file, and so on, until the last line of the
longer file (in terms of lines) is printed.
b. Modify the program so that lines with the same line number are printed on
the same line.
13. Write a program that takes a character and a filename. Have it open the file
and report how many times the character appears in the file. The filename and
the character itself should be reported along with the count. If the file can’t be
opened, have the program report that fact.
14. Create a text file consisting of 20 rows of 30 integers. The integers should be in
the range 0-9 and be separated by spaces. The file is a digital representation of
a picture, with the values 0 through 9 representing increasing levels of darkness.
Write a program that reads the contents of the file into a 20-by-30 array of ints.
In a crude approach toward converting this digital representation to a picture,
have the program use the values in this array to initialize a 20-by-31 array of
chars, with a 0 value corresponding to a space character, a 1 value to the period
character, and so on, with each larger number represented by a character that
occupies more space. For example, you might use # to represent 9. The last
character (the 31st) in each row should be a null character, making it an array
of 20 strings. Have the program display the resulting picture (that is, print the
strings) and also store the result in a text file. For example, suppose you start
with this data given in Figure 1.
For one particular choice of output characters, the output looks like Figure 2.
Page 3
Figure 1: Input data
Figure 2: Output data
Page 4
15. Write a program that reads a file consisting of students’ test scores in the range
0200. It should then determine the number of students having scores in each of
the following ranges: 024, 2549, 5074, 7599, 100124, 125149, 150174, and 175200.
Output the score ranges and the number of students. (Run your program with
the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189,
167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)
16. The history teacher at your school needs help in grading a True/False test. The
students’ IDs and test answers are stored in a file. The first entry in the file
contains answers to the test in the form:
TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed by
the students responses. For example, the entry:
ABC54301 TFTFTFTT TFTFTFFTTFT
indicates that the student ID is ABC54301 and the answer to question 1 is True,
the answer to question 2 is False, and so on. This student did not answer question
9. The exam has 20 questions, and the class has more than 150 students. Each
correct answer is awarded two points, each wrong answer gets one point deducted,
and no answer gets zero points. Write a program that processes the test data. The
output should be the student’s ID, followed by the answers, followed by the test
score, followed by the test grade. Assume the following grade scale: 90%-100%,
A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; and 0%-59.99%, F.
Page 5