-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.cpp
73 lines (61 loc) · 1.33 KB
/
lab2.cpp
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
// Kaitlyn Lavan
// 10/08/2018
// cisc2200
// checks if the input string is a palindrome
// ** fix all cases of error with stack overflow
#include <iostream>
#include <stack>
using namespace std;
string ProcessInput (string s)
{
int len = s.length();
string alnum_str = "";
// checks if each character in a string is either alpha or num and a
// appends the char to a new string if it is
for (int i=0; i < len; i++)
{
if(isalnum(s[i]))
{
if (isalpha(s[i]))
s[i] = tolower(s[i]);
alnum_str = alnum_str + s[i];
}
}
return alnum_str;
}
int main()
{
//variables
string input_str, processed_str;
stack<char> palstack;
int str_len;
bool palidrome = true;
cout << "Enter a string to check if it is palidrome!" << endl;
getline(cin, input_str);
// process input_string
processed_str = ProcessInput(input_str);
str_len = processed_str.length();
// *** check for stack overflow
for (int i =0; i < str_len; ++i)
{
palstack.push(processed_str[i]);
}
for (int i=0;i < (str_len/2);i++)
{
if (palstack.top() == processed_str[i])
{
if (!palstack.empty())
palstack.pop();
}
else
{
palidrome = false;
break;
}
}
if (palidrome)
cout << "Your input is a palidrome!" << endl;
else
cout << "Your input is not a palindrome." << endl;
return 0;
}