
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define ASCIITOP  123

void zero(int letterfreq[]);
int howbig(char *filenamePtr);
char *ReadString(char *filename, int *lengthPTR);
/* void analysis(char *string, int); */
/* void bubsort(int x[26],int y[26]); */

main ()

{
    FILE *ifp;
    char *string, *fbegin, filename[20];
    int ch, i, other, * lengthPTR, length = 0,
         letterfreq[123];            /* how many of each letter */

    /* what is the filename..ask user */
    lengthPTR = (int *)malloc(sizeof(int));
    *lengthPTR = 0;
    printf("Enter the name of the text file to be examined: ");
    scanf ("%s", filename);
    printf ("\n");
    zero(letterfreq);            /* initialize freq. array to 0 */
    length = howbig(filename);   /* how big is this file anyway ??*/
   
    *lengthPTR = length;
    string = ReadString(filename, lengthPTR);   /*put contents in array */
    length = length + 2;                    /* white space and /0 added */
    /* analysis(string, length); */               /* let's analyze it */ 
 

    close(ifp);
}

  
void zero(int letterfreq[123])
{
    int i;
    for (i=0;i<=122;i++)
      letterfreq[i] = 0;
}



  char *ReadString(char *filename, int *lengthPTR)
{
  int i,length=0;
  char * string;
  FILE *ifp;
  length = *lengthPTR;
  string = (char *)malloc((length + 1)*sizeof(char));  /*allocate memory */
  if (string == NULL)
  {
    fprintf(stderr, "Memory allocation failed for string\n");
    exit(-1);
  }
    ifp = fopen(filename, "r");
    if (ifp == NULL)             /* bad news ! */
      {
          printf("can't open the file ");
          exit(-1);            
      }

    /* process this file... */
 
    for (i=1;i<= length;i++)
      string[i] = fgetc(ifp);         /* read each character into array */
    string[i] = ' ';               /*add space after last word */
    string[i+1] = '\0';                /*put  null terminator at end */
    rewind(ifp); 
   
    return string;
}


int howbig(char *filename)     /* finds out how many chars in file */
{
    int ch, j=0,length = 0;
    FILE *ifp;
    ifp = fopen(filename, "r");
    if (ifp == NULL)             /* bad news ! */
      {
          printf("can't open the file ");
          exit(-1);            
      }

    /* process this file.....ch must be an int */
     while ((ch = fgetc(ifp)) != EOF)
      { length++;
        printf("%d",ch);
        j = j + 1;
        if (j % 10 == 0)
            printf(" \n");
      }
     rewind(ifp);               /* better rewind the file  */
     close(ifp);               /* and close it  */
     return length;
}







