/*
 Ross Bullock
 Assignment 4
 CPSC 1010 - Section 2
 Due: December 2, 2015
 */

#include <stdio.h>
#include "functions.h"
#include <ctype.h>

int main(int argc, char *argv[])
{
    FILE *in;
    FILE *out;
    in = fopen(argv[1], "r");
    out = fopen(argv[2], "w");
    char c, d;
    int numbers[100];
    int numWords = 0;
    int numSentences = 0;
    while((c = getc(in)) != EOF)
    {
        d = swap_case(c);
        putc(d, out);
        //fprintf(stdout, "%c", c);
        //rewind?
    }
    int i = 0;
    while((c = getc(in)) != EOF)
    {
        if(isdigit(c))
        {
            ungetc(c, in);
            fscanf(stdin, "%d", &numbers[i]);
            i++;
        }
        //rewind?
    }
    while((c = getc(in)) != EOF)
    {
        if(getc(in) == ' ')
        {
            numWords++;
        }
        //rewind?
    }
    while((c = getc(in)) != EOF)
    {
        if((getc(in) == '.') || (getc(in) == '?') || (getc(in) == '!'))
        {
            numSentences++;
        }
        fprintf(stdout, "%d", numSentences);
        //rewind;
    }
    
    fprintf(stdout, "\n***************************************************************\n");
    fprintf(stdout, "Below is the text that, for each character, the case has been changed. There are also integers included in this text. The integer value has not been altered.\n");
    fprintf(stdout, "***************************************************************\n");
    while((c = getc(out)) != EOF)
    {
        fprintf(stdout, "%c", c);
    }
    fprintf(stdout, "***************************\n");
    fprintf(stdout, "The word count is: %d\n", numWords);
    fprintf(stdout, "The sentence count is: %d\n", numSentences);
    fprintf(stdout, "***************************\n");
    fprintf(stdout, "Below is a list of the \n integers scattered in this\n file\n");
    fprintf(stdout, "***************************\n");
    int j;
    for(j = 0; j < i; j++)
    {
        fprintf(stdout, "%d\n", numbers[j]);
    }
    fprintf(stdout, "***************************\n");
    fprintf(stdout, "The sum is: %d\n", sum(numbers, i));
    fprintf(stdout, "The avg is: %d\n", avg(numbers, i));
    
    
    return 0;
}