Tutorial 5: Pointers and Dynamic Memory
| 1. |
Write a program that replaces the most commonly occurring character in a file with another character.
For example:
csse2100% cat SampleFile
This is a sample file. It contains some lines.
Some longer than others; some shorter than others.
Not a strange file needless to say, except that it has a lot of o's in it:
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Yes, that was them.
csse2100% ./replace SampleFile $
This is a sample file. It c$ntains s$me lines.
S$me l$nger than $thers; s$me sh$rter than $thers.
N$t a strange file needless t$ say, except that it has a l$t $f $'s in it:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Yes, that was them.
Your program should accept two arguments: the name of the file to process and the character that should be used to replace the most commonly occurring character in the file.
Ensure that your program only reads the file once,
storing the input in dynamically allocated memory
when reading the file,
and then modifying the stored input before it is printed.
|
|