/* Spring 2023 - PA2.c

	continuation of Lab 8

	Splits up the functions into separate files.
	Adds a word search function to the program.
	The menu will now be the following:
		1. print the poem 
		2. show number of lines in the poem
		3. convert the case
		4. search for a word 
		5. quit the program	

*/


#include "defs.h"


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 file, read in text into theText array
	//		get the numLines while reading in the text
	//		copy that into oppositeCase array

	// 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 poem
		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;
}


