-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedition5chapt9_.cpp
88 lines (74 loc) · 1.64 KB
/
edition5chapt9_.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstring>
#include <cctype>
// the library cstring is for string processing.
// cctype supports more functions
#include <string>
using namespace std;
void swap(char& v1, char& v2);
string reverse(const string& s);
string removePunct(const string& s, const string& punct);
string makeLower(const string& s);
bool isPalindrome(const string& s);
int main()
{
string s1, s2("Hello ");
cout << "Enter a line of input:\n";
getline(cin, s1);
if (s1 == s2 )
cout << "Equal\n";
else
cout << "Not equal\n";
return 0;
}
void swap(char& v1, char& v2)
{
char temp;
temp = v1;
v1 = v2;
v2 = temp;
}
string reverse(const string& s)
{
int start = 0;
int end = s.length();
string temp(s);
while (start < end)
{
end--;
swap(temp[start], temp[end]);
start++;
}
return temp;
}
string makeLower(const string& s)
{
string temp(s);
for (int i = 0; i < s.length(); i++)
{
temp[i] = tolower(s[i]);
}
return temp;
}
string removePunct(const string& s, const string& punct)
{
string noPunct;
int sLength = s.length();
int punctLength = punct.length();
for (int i = 0; i < sLength; i++)
{
string aChar = s.substr(i, 1);
int location = punct.find(aChar, 0);
if (location < 0 || location >= punctLength)
noPunct = noPunct + aChar;
}
return noPunct;
}
bool isPalindrome(const string& s)
{
string punct(",;:.?!'\" ");
string str(s);
str = makeLower(str);
string lowerStr = removePunct(str, punct);
return(lowerStr == reverse(lowerStr));
}