Reverse a file

Chapter: Files
...Section: Reverse a file

We haven't studied stacks in C yet, so to reverse a file, let's simply read every alphabetic char (together with spaces and newlines) into an array of char, say char text[MAX] where you use #define to specify the constant MAX. Then print it out backwards using

  for (i = count-1; i>=0; i--) {
    printf("%c",text[i]);
  }


Exercise 2

Write a C program to read the alphabetic, space, and newline characters in a file into an array and then print the array backwards. Sample input might be the Gettysburg address producing output like this

htrae eht morf hsirep
ton llahs elpoep eht rof elpoep eht yb elpoep eht fo tnemnrevog
taht dna  modeerf fo htrib wen a evah llahs doG rednu noitan siht
taht  niav ni deid evah ton llahs daed eseht taht evloser ylhgih
ereh ew taht  noitoved fo erusaem lluf tsal eht evag yeht hcihw
rof esuac taht ot noitoved desaercni ekat ew daed deronoh eseht morf
taht  su erofeb gniniamer ksat taerg eht ot detacided ereh eb ot
su rof rehtar si tI decnavda ylbon os raf suht evah ereh thguof ohw
yeht hcihw krow dehsinifnu eht ot ereh detacided eb ot rehtar gnivil
eht su rof si tI ereh did yeht tahw tegrof reven nac ti tub ereh yas
ew tahw rebmemer gnol ron eton elttil lliw dlrow ehT tcarted ro dda
ot rewop roop ruo evoba raf ti detarcesnoc evah ereh delggurts ohw
daed dna gnivil nem evarb ehT dnuorg siht  wollah ton nac ew 
etarcesnoc ton nac ew  etacided ton nac ew esnes regral a ni tuB

siht od dluohs ew taht reporp dna
gnittif rehtegotla si tI evil thgim noitan taht taht sevil rieht evag
ereh ohw esoht rof ecalp gnitser lanif a sa dleif taht fo noitrop
a etacided ot emoc evah eW raw taht fo dleifelttab taerg a no tem
era eW erudne gnol nac detacided os dna deviecnoc os noitan yna ro
noitan taht rehtehw gnitset raw livic taerg a ni degagne era ew woN

lauqe detaerc era nem lla taht noitisoporp
eht ot detacided dna ytrebiL ni deviecnoc noitan wen a tnenitnoc
siht no htrof thguorb srehtaf ruo oga sraey neves dna erocs ruoF



rhyspj@gwu.edu