/* Spring 2023 - Lab 8

	continuation of Lab 7

	This program will include user-defined functions for some of the tasks

	The following functions will be implemented, and the code for each moved 
	from the main function into their respective functions.
		1. printMenu
		2. printArray
		3. convertCase

*/


#include <stdio.h>
// #include <string.h>

#define MAX_LINE 200
#define MAX_LINE_LEN 100

int printMenu();
void printArray(char text[MAX_LINE][MAX_LINE_LEN], int size);
void convertCase(char text[MAX_LINE][MAX_LINE_LEN]);


int main(void) {
	char theText[MAX_LINE][MAX_LINE_LEN];
	char oppositeCase[MAX_LINE][MAX_LINE_LEN]; 
	int i = 0, j; 
	int menuChoice = 0, numLines = 0;  // wordCount = 0, occurences = 0;
	FILE *inFile = fopen("input.txt", "r");
	
	/* ----------------------------------------------------------------- */
	// get some things initialized before entering loop:
	//		open input read in text into theText
	//		copy that into oppositeCase
	// 	reset stdin so scanf() will work

	// fill up array with lines from file
	while ((fgets(theText[i], MAX_LINE_LEN, inFile) != NULL)) {
		i++;
	}
	// the value of i is the number of lines
	numLines = i;

	// copy into oppositeCase so original array is preserved
	for (i = 0; i < MAX_LINE; i++ ) {
		for (j = 0; j < MAX_LINE_LEN; j++) 
			oppositeCase[i][j] = theText[i][j];
	}

	// freopen("/dev/tty", "rw", stdin);  // resets stdin so scanf() will work
	/* ----------------------------------------------------------------- */

	while(menuChoice != 4) {
		menuChoice = printMenu();


		// print the array
		if (menuChoice == 1)
			printArray(theText, numLines);

		// print the number of lines in the text
		else if (menuChoice == 2)
			printf("\nThe poem has %d lines.\n\n", numLines);

		// convert the case
		else if (menuChoice == 3) {
			convertCase(oppositeCase);

			// print the array
			printArray(oppositeCase, numLines);
		}
	}

	return 0;
}


/* ------------------------------------------------------------ */
/* prints a menu to the user
   parameters:  none
	return:  the menu choice that the user enters
*/
int printMenu() {
	int choice = 0;
	do {
		// print a menu to the user
		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", &choice);
	} while( (choice != 1) && (choice != 2) &&
			   (choice != 3) && (choice != 4) ); 

	return choice;
}
/* ------------------------------------------------------------ */


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


/* ------------------------------------------------------------ */
/* converts the case of the characters in the array sent in 
   parameters:  the array 
	return:  none  
*/
void convertCase(char text[MAX_LINE][MAX_LINE_LEN]) {
	for (int i = 0; i < MAX_LINE; i++ ) {
		for (int j = 0; j < MAX_LINE_LEN; j++) {
			if (text[i][j] < 91 && text[i][j] > 64)
				text[i][j] += 32;
			else if (text[i][j] < 123 && text[i][j] > 96)
				text[i][j] -= 32;
		}
	} 
}
/* ------------------------------------------------------------ */






