/*Samuel Curry
spring cpsc 1011
009
3/8/2023
This program tskes a number from the user and then either print the text, count the number of lines or get opposite cases. 
*/
#include <stdio.h>
#include <string.h>


int main (void){
	//first variables used for the arrays and other needed ints
	int User_input = 0, contin = 1;
	int count = 0;
	const int Max_Line = 200, Max_len = 100;
	char lines[Max_len + 1];
	char array1[Max_Line][Max_len + 1], oppositeCase[Max_Line][Max_len];
	// establishing a file pointer.
	FILE *inFile;
	char filename[] = "input.txt";
	// while loop for cotinuity
	while (contin == 1){

		inFile = fopen(filename, "r");
		//starts to grab and read the file
		while (fgets(lines, sizeof(lines), inFile)) {
			//if it reaches the max number then break
			if (count >= Max_Line) {
				break;
			}
			//stores the info on array1
			strncpy(array1[count], lines, sizeof(array1[0])-1);
			array1[count][Max_len] = '\0';
			count++;
		} 

		fclose(inFile);
		// copies info from array1 to oppositeCase
		for (int i = 0; i < count; i++){
			for (int j = 0; j < Max_len; j++){
				oppositeCase[i][j] = array1[i][j];
			}
		}

		//grabbing user input
		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", &User_input);
		printf("\n");

		// prints the txt
		if (User_input == 1){

			for (int i = 0; i < count; i++){
				printf("%s",array1[i]);
			}
		}
		//prints the number of lines
		else if(User_input == 2){
			printf("The poem has %d lines.\n", count);
		}
		// changes the case for the letters/upper/lower
		else if(User_input == 3){
			for (int i = 0; i < count; i++){
				char* str = oppositeCase[i];
				for (int j = 0; j < strlen(str); j++){
					if (str[j] >= 'A' && str[j] <= 'Z'){
						str[j] += ('a' - 'A');
					}
					else if (str[j] >= 'a' && str[j] <= 'z'){
						str[j] += ('A' - 'a');
					}
				}
			}
			//prints the oppositeCase for user
			for (int i = 0; i < count; i++){
				printf("%s", oppositeCase[i]);
			}			
		}
		//leaves the loop and stops code
		else if(User_input == 4){
			contin = 0;
			
		}
	}
	return 0;

}
