// Tyler Taormina // CST 116 // Module 11c // #include #include #include #define MAX 50 using namespace std; void isPalindrome(); void isAlpha(); void countChar(); void DisplayMenu(int&); void ProcessMenuChoice(int); int main() { int user_choice; cout << "=================================================================" << endl; cout << "PROGRAM RUNNING.." << endl; cout << "=================================================================" << endl; cout << endl; DisplayMenu(user_choice); ProcessMenuChoice(user_choice); return 0; } void DisplayMenu(int& user_choice) { cout << "Choose what you would like to check for in a word" << endl; cout << "that you will enter..." << endl; cout << "=================================================================" << endl; //displays the menu of functions for the user to choose from cout << "1) Check for palindrome.\n"; cout << "2) Check for all alpha.\n"; cout << "3) Count the number of times a letter is in a word.\n"; cout << "4) Exit Program.\n\n"; cout << "Enter: "; cin >> user_choice; if (user_choice > 4 || user_choice< 1) { cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; DisplayMenu(user_choice); } } void ProcessMenuChoice (int menu_choice) { // Uses the user menu choice input to determine which function to call. // Also controls the ending or restarting of program. int program_rerun = 0; switch(menu_choice){ case 1: isPalindrome(); break; case 2: isAlpha(); break; case 3: countChar(); break; case 4: cout << "Are you sure you want to exit? Enter any number other than 1 to end program." << endl; break; default: break; } cout << "Press 1 and enter to rerun program. Enter any other number to close program: "; cin >> program_rerun; if (program_rerun == 1) main(); else { cout << "================================================================" << endl; cout << "Program Closing..." << endl; cout << "================================================================\n\n\n" << endl; } } void isPalindrome () { char string1[MAX]; int i, length; int flag = 0; cout << "Enter a string: "; cin >> string1; length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { cout << string1 << " is not a palindrome" << endl; } else { cout << string1 << " is a palindrome" << endl; } } void isAlpha () { char str[MAX]; int i; cout << "Enter a string to test: "; cin >> str; for (int i = 0; str[i] != '\0'; i++) { if (!isalnum(str[i])) { cout << str[i] << " is not alphanumeric" << endl; } } } void countChar () { std::string string1; char ch; cout << "Please enter a word: "; cin >> string1; cout << "Please enter a character to count: "; cin >> ch; int count = 0; for (int i = 0; (i = string1.find(ch, i)) != std::string::npos; i++) { count++; } std::cout << "Character " << ch << " occurs " << count << " times" << endl << endl; }