/***************************
 *Jessica Qiu
 *Assignment 4
 *CPSC 101 - Section 001
 *Due: December 2, 2015
***************************/

#include "functions.h"
#include <stdio.h>

//changes to uppercase
char make_upper(char c)
{
   if(c >= 97 && c <= 122)
   {
      c-=32;	    
   }	
   return c;
}

//changes to lowercase
char make_lower(char c)
{
   if(c >= 65 && c <= 90)	
   {
      c+=32;
   }
   return c;
}

//counts the words in the file
int countWords(FILE *inPtr)
{
   char ch;
   int count = 0;

   while((ch = fgetc(inPtr)) != EOF)
   {
      if(ch == ' ')
      {
	 count++;
      }
   }

   return count; 
}

//counts the sentences in the file
int CountSentences(FILE *inPtr)
{
   char end;
   int count = 0;

   while((end = fgetc(inPtr)) != EOF)
   {
      if(end == '.' || end == '!' || end == '?')
      {
	 count++;
      }
   }

   return count; 
}

//finds the scattered integers in the file
void findNumber(FILE *inPtr, FILE *outPtr)
{
   int counter = 0;   
   int numArray[10];

   while(fscanf(inPtr, "%d\n", &numArray[counter]) != EOF)
   {  	   
      counter++;
   }

   int index;
   for(index=0;index<counter;index++)
   {
      fprintf(outPtr, "%d \n", numArray[index]);
   }
}

//adds all the integers in the file
void sum(FILE *inPtr, FILE *outPtr)
{
   int counter = 0;
   int total = 0;   
   int numArray[10];

   while(fscanf(inPtr, "%d\n", &numArray[counter]) != EOF)
   {  	   
      total += numArray[counter];

      counter++;
   }

   fprintf(outPtr, "Sum: %d\n", total);
}

//finds the average of all the integers in the file
void average(FILE *inPtr, FILE *outPtr)
{
   int counter = 0;
   int total = 0;   
   int numArray[10];

   while(fscanf(inPtr, "%d\n", &numArray[counter]) != EOF)
   {  	   
      total += numArray[counter];

      counter++;
   }

   total = total/7;

   fprintf(outPtr, "Average: %d\n", total);
}

