/* Spring 2023 - Lab 7

	continuation of Lab 6
	this program will add a menu with choices:
		1. printing the poem 
		2. counting the number of lines in the poem
		3. printing the poem where the characters are the opposite case
		4. quit the program	

*/


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


#define MAX_LINE 200
#define MAX_LINE_LEN 100

int main(void) {
	char theText[MAX_LINE][MAX_LINE_LEN];
	char oppositeCase[MAX_LINE][MAX_LINE_LEN]; 
	int i = 0, j, k; 
	int menuChoice = 0, numLines = 0;  // wordCount = 0, occurences = 0;
	FILE *inFile = fopen("poe-raven.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) {
		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", &menuChoice);
			printf("\n");
		} while( (menuChoice != 1) && (menuChoice != 2) &&
				   (menuChoice != 3) && (menuChoice != 4) ); 


		// print the array
		if (menuChoice == 1) {
			for (i = 0; i < numLines; i++) {
				printf("%s", theText[i]);
			}
		}

		else if (menuChoice == 2)
			printf("\nThe poem has %d lines.\n\n", numLines);

		else if (menuChoice == 3) {
			// convert the case
			for (i = 0; i < MAX_LINE; i++ ) {
				for (j = 0; j < MAX_LINE_LEN; j++) {
					if (oppositeCase[i][j] < 91 && oppositeCase[i][j] > 64)
						oppositeCase[i][j] += 32;
					else if (oppositeCase[i][j] < 123 && oppositeCase[i][j] > 96)
						oppositeCase[i][j] -= 32;
				}
			}

			// print the array
			for (i = 0; i < numLines; i++) {
				printf("%s", oppositeCase[i]);
			}
		}
	}

	return 0;
}








