//3/15/2023
//Section 1
// Robert Magnuson
//Lab 8
//This program reads a file containing the Raven, prints the number of lines and then the text itself
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE_LEN 100
#define MAX_LINE 200
void printArray(char str[MAX_LINE][MAX_LINE_LEN], int size);
int printMenu();
void convertCase (char oppositeCase[MAX_LINE][MAX_LINE_LEN], int size);
int printMenu(){
    int menuChoice;
    printf("\nChoose from the menu:\n");
    printf("\t1. print the poem\n");
    printf("\t2. show number of lines in the poem\n");
    printf("\t3. convert the case\n");
    printf("\t4. quit\n\n");
    printf("\t - - > ");
    scanf("%d", &menuChoice);
    printf("\n");
    return menuChoice;
}
void printArray(char str[MAX_LINE][MAX_LINE_LEN], int size){
    FILE *inFile;
    inFile = fopen("input.txt", "r");
    if (inFile == NULL)
    {
        printf("error");
        exit(0);
    }
    size = 0;
    while (!feof(inFile) && !ferror(inFile)){
        if (fgets(str[size], MAX_LINE_LEN, inFile) != NULL){
            size++;
        }
    }
    fclose(inFile);
    for (int m = 0; m < size; m++){
        printf("%s", str[m]);
    }
}
void convertCase (char oppositeCase[MAX_LINE][MAX_LINE_LEN], int size){
    FILE *inFile;
    inFile = fopen("input.txt", "r");
    if (inFile == NULL)
    {
        printf("error");
        exit(0);
    }
    size = 0;
    while (!feof(inFile) && !ferror(inFile)){
    if (fgets(oppositeCase[size], MAX_LINE_LEN, inFile) != NULL){
        size++;
    }
    }
    fclose(inFile);
    for (int i = 0; i < MAX_LINE; i++) {
        for (int j = 0; j < MAX_LINE; j++) {
            if (oppositeCase[i][j] >= 'a' && oppositeCase[i][j] <= 'z'){
                oppositeCase[i][j] -= 32;
            }
            else if (oppositeCase[i][j] >= 'A' && oppositeCase[i][j] <= 'Z'){
                oppositeCase[i][j] += 32;
            }
        }
    }
    for (int g = 0; g < size; g++){
        printf("%s", oppositeCase[g]);
    }
}
int main () {
    int size = 0;
    int t;
    t = 0;
    int menuChoice;
    menuChoice = printMenu();
    char oppositeCase[MAX_LINE] [MAX_LINE_LEN];
    char str[MAX_LINE][MAX_LINE_LEN];
    while (t == 0){
        if (menuChoice == 1){
            printArray(str, size);
        }
        if (menuChoice == 2){
            printf("The poem has %d lines.\n", size);
        }
        if (menuChoice == 3){
            convertCase (oppositeCase, size);
        }
    }
    if (menuChoice == 4){
        t++;
        exit(0);
    }
}