
/*
 * Name: Jonathan Flander
 * Date: March 10, 2023
 * Lab Section: cpsc 1011-004
 * Lab Number: 7
 * Lab 7 - Program description: This program reads text from a file, counts the number of lines, 
 *                              and allows the user to choose from a menu to display the text, show the number of lines, 
 *                              or convert the case of the characters.
   
*/


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

#define MAX_LINE 200
#define MAX_LINE_LEN 100

int main() {
    char text[MAX_LINE][MAX_LINE_LEN];
    char oppositeCase[MAX_LINE][MAX_LINE_LEN];
    int line_count = 0, menu_choice = 0;

    // Open input file
    FILE *inFile = fopen("input.txt", "r");

    // Read in text from input file
    while (fgets(text[line_count], MAX_LINE_LEN, inFile) != NULL) {
        line_count++;
    }

    // Copy text to oppositeCase array
    for (int i = 0; i < line_count; i++) {
        for (int j = 0; j < MAX_LINE_LEN; j++) {
            oppositeCase[i][j] = text[i][j];
        }
    }

    // Print menu and get user input
    while (menu_choice != 4) {
        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", &menu_choice);
        printf("\n");

        switch (menu_choice) {
            case 1:
                // Print text
                for (int i = 0; i < line_count; i++) {
                    printf("%s", text[i]);
                }
                break;
            case 2:
                // Print number of lines
                printf("The poem has %d lines.\n", line_count);
                break;
            case 3:
                // Convert case of oppositeCase array and print
                for (int i = 0; i < line_count; i++) {
                    for (int j = 0; j < MAX_LINE_LEN; j++) {
                        oppositeCase[i][j] = isupper(oppositeCase[i][j]) ? tolower(oppositeCase[i][j]) : toupper(oppositeCase[i][j]);
                    }
                    printf("%s", oppositeCase[i]);
                }
                break;
            case 4:
                // Quit program
                break;
            default:
                // Invalid input, do nothing
                break;
        }
    }

    // Close input file
    fclose(inFile);

    return 0;
}
