/* 
Name: Kevin Blinn
Date: 3/14/23
Lab Section: CPSC 1011-001
Lab Number: Lab 8) Functions
Program:
   This program is a continuation of lab7. This lab will provide a menu to the user that gives them the 
	choice to 1. Print the poem, 2. Show number of lines in the poem, 3. convert the case 
	by using functions.
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h> // library found on cplusplus.com

const int MAX_LINE = 200, MAX_LINE_LEN = 100;

// Function  Prototypes
int printMenu();
void printArray(char lines[MAX_LINE][MAX_LINE_LEN], int size);
void convertCase(char lines[MAX_LINE][MAX_LINE_LEN]);

int main(void) {
	char lines[MAX_LINE][MAX_LINE_LEN];  //oppositeCase[MAX_LINE][MAX_LINE_LEN];
	int  line = 0, menuChoice, loopCheck = 0;

// File pointer
	FILE *inFile = fopen("input.txt", "r"); 

// Puts lines in the array and counts lines
   while(fgets(lines[line], MAX_LINE, inFile)) {
   line++;
   }

// Display menu for the user
	do{
		menuChoice = printMenu();
		switch (menuChoice) {
			case 1: // Print the txt
   			printArray(lines, line);
				break;
			case 2: // Counts the lines
				printf("The poem has %d lines.\n", line);
            break;
			case 3: // Convert the case
				convertCase(lines);
            break;
			case 4: // Quits the loop
				loopCheck = 4;
            break;

		}

	} while (loopCheck != 4);


fclose(inFile);
return 0;

}

/* ---------------------------------------- */
/* prints a menu to the user
   parameters: none
	return: the menu choice that the user enters
*/
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;
}

/* ---------------------------------------- */
/* prints the contents of the array 
   parameters: the array, the number of lines in the array
   return: None
*/
void printArray(char lines[MAX_LINE][MAX_LINE_LEN], int size){
	for (int i = 0; i < size; i++) {
		printf("%s", lines[i]);
		}
}

/* ---------------------------------------- */
/* prints the contents of the array with opposite case
   parameters: the array, the number of lines in the array
   return: None
*/
void convertCase(char lines[MAX_LINE][MAX_LINE_LEN]){
	char oppositeCase[MAX_LINE][MAX_LINE_LEN];
	for (int i = 0; i < MAX_LINE; i++){
		for (int j = 0; j < MAX_LINE_LEN; j++) {
			oppositeCase[i][j] = lines[i][j];
			}
		}
	for (int i = 0; i < MAX_LINE; i++){
		for (int j = 0; j < MAX_LINE_LEN; j++) {
			if (isupper(oppositeCase[i][j])){
				oppositeCase[i][j] = tolower(oppositeCase[i][j]);
				}
			else if (islower(oppositeCase[i][j])){
				oppositeCase[i][j] = toupper(oppositeCase[i][j]);
				}
			}
		}
	for (int i = 0; i < MAX_LINE; i++) {
		printf("%s", oppositeCase[i]);
		}
}
