blob: 2ce45b5b691f7a406646f17542b3cc464b7c5120 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Tyler Taormina
// CST 116
// Module 11c
//
#include <iostream>
#include <cstring>
#include <string>
#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;
}
|