-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_basic.cpp
More file actions
61 lines (55 loc) · 1.35 KB
/
palindrome_basic.cpp
File metadata and controls
61 lines (55 loc) · 1.35 KB
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
#include <iostream>
#include <cstring>
// to save time, let's lump all the cleaning into one loop
std::string cleanString(std::string &dirty)
{
std::string clean = "";
for (int i = 0; i < dirty.length(); i++)
{
dirty[i] = tolower(dirty[i]);
if (isspace(dirty[i]))
continue;
else
clean[i] = dirty[i];
}
return clean;
}
// inplace algorithm for determining if a string is a palindrome
bool isPalindrome(std::string pal)
{
int left, right;
left = 0;
right = pal.length() - 1;
while (left <= right)
{
if (pal[left] != pal[right])
{
return 0;
}
++left;
--right;
}
return 1;
}
int main()
{
// create batch of test cases
std::string test1 = "racecar";
std::string test2 = "Civic";
std::string test3 = "Hannah";
std::string test4 = "Never odd or even";
std::string test5 = "Mad Adam";
// clean the string
test1 = cleanString(test1);
test2 = cleanString(test2);
test3 = cleanString(test3);
test4 = cleanString(test4);
test5 = cleanString(test5);
// print the palindrome
std::cout << isPalindrome(test1) << std::endl;
std::cout << isPalindrome(test2) << std::endl;
std::cout << isPalindrome(test3) << std::endl;
std::cout << isPalindrome(test4) << std::endl;
std::cout << isPalindrome(test5) << std::endl;
return 0;
}