Starting from:

$34.99

CS1010S Tutorial 8 Solution

File I/O
1. File Analysis
For each of the following task, write a function that takes in a FILE stream and outputs the required result:
(a) Count the number of alphabatical letters in the file.
(b) Count the number of words in the file.
(c) Count the number of sentences in the file. You can assume that all sentences end with a period ‘.’.
2. Transforming Files
For each of the following text transformations, write a function that takes two FILE pointers to text files, one for input and one for output, and copies the contents of the input to the output while performing the transform.
(a) Changes all lowercase letters to uppercase.
(b) Capitalize the first letter of every word. Other capital letters in the input will remain as is.
(c) Reverse the contents of each line.
For example:
No one can tell me,
Nobody knows,
Where the wind comes from, Where the wind goes. will become ,em llet nac eno oN
,swonk ydoboN
,morf semoc dniw eht erehW
.seog dniw eht erehW'
(d) Capitalize the first letter of the first word of a sentence. You can assume that a sentence ends with a period ‘.’. Note that a sentence can span multiple lines.
3. Write a function that reads from two text files, and outputs a new text file that interleaves the lines from the first two files. That is, the output file will contain the first line from the first file, then the first line from the second, then the second from the first, then second from the second and so on.
1
4. Write a function that takes in two file names as inputs, and copies the contents of the first file to the second. The function should be able to perfectlt duplicate any files, text or binary.

More products