diff options
| author | Tyler Taormina <[email protected]> | 2021-11-10 19:13:28 -0800 |
|---|---|---|
| committer | Tyler Taormina <[email protected]> | 2021-11-10 19:13:28 -0800 |
| commit | 2eda75e6d57723e935624f88f6c89bf9000df7a7 (patch) | |
| tree | edb60636201ca4a52dec3e257fdec81fdf7fb547 | |
| parent | Daily edit for lab. (diff) | |
| download | cst116-lab6-till-t-2eda75e6d57723e935624f88f6c89bf9000df7a7.tar.xz cst116-lab6-till-t-2eda75e6d57723e935624f88f6c89bf9000df7a7.zip | |
countChar function is working!
Tyler
| -rw-r--r-- | mod11c.cpp | 83 |
1 files changed, 53 insertions, 30 deletions
@@ -4,45 +4,39 @@ // #include <iostream> -#include <iomanip> +#include <cstring> +#include <string> + +#define MAX 50 using namespace std; -void isPalindrome(string); -void isAlpha(string); -void countChar(string); -void getData(string&); +void isPalindrome(); +void isAlpha(); +void countChar(); void DisplayMenu(int&); -void processChoice(int, string); +void ProcessMenuChoice(int); int main() { - string user_string; int user_choice; cout << "=================================================================" << endl; cout << "PROGRAM RUNNING.." << endl; cout << "=================================================================" << endl; cout << endl; - getData(user_string); + DisplayMenu(user_choice); - processChoice(user_choice, user_string); + ProcessMenuChoice(user_choice); return 0; } -void getData(string& usr_data) -{ - cout << "Lets take a look at how to 'check' a string..." << endl; - cout << "Please enter a word: "; - cin >> usr_data; -} - void DisplayMenu(int& user_choice) { - cout << "Choose what you would like to check for in the string" << endl; - cout << "that you entered..." << endl; + 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 @@ -60,25 +54,25 @@ void DisplayMenu(int& user_choice) -void ProcessMenuChoice (int menu_choice, string user_str) { +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(user_str); + isPalindrome(); break; case 2: - isAlpha(user_str); + isAlpha(); break; case 3: - countChar(user_str); + countChar(); break; case 4: - cout << "Are you sure you want to exit? Enter 1 to end program." << endl; + cout << "Are you sure you want to exit? Enter any number other than 1 to end program." << endl; break; default: @@ -98,24 +92,53 @@ void ProcessMenuChoice (int menu_choice, string user_str) { -void isPalindrome (string usr_data) +void isPalindrome () { - cout << "is palindrome" << endl; + 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 (string usr_data) +void isAlpha () { cout << " alphabet check" << endl; } -void countChar (string usr_data) +void countChar () { - cout << "Counting characters.." << endl; -} - + 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; +} |