FANDOM


/* Lab 8c */
/* Kevin Duke and Tomi Babalola*/
#include <stdio.h>
#include <string.h>
 
void reversestring(char file[])
{
 
  char run=0;
  int counter=0;
  int i=0;
  /*initialize the original array*/
  char original[100];
  FILE *infilep;
 
  infilep = fopen(file, "r");
/*if file doesn't exist*/
  if(infilep==NULL)
    printf("Invalid file\n");
  else
  /*while it's not the end of file*/
    while(run != EOF)
      {
          /*print them in the original array*/
        run = fgetc(infilep);
        printf("%c", run);
        original[counter]=run;
        counter++;
      }
  counter=counter-2;
 
  printf("\n\nThe file reversed:\n");
  i=0;
  /*reverse original array*/
  while(i <= counter)
    {/*print*/
     printf("%c",original[counter-i]);
     i++;
    }
  printf("\n");
  return;
}
 
int main(void)
{
  char file[70];
 
  printf("Name of the file to print: ");
  gets(file);
  printf("\nThe file before reversing:\n");
  reversestring(file);
 
  return 0;
}