-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathpalindromeChecker.cpp
More file actions
49 lines (43 loc) · 1.22 KB
/
palindromeChecker.cpp
File metadata and controls
49 lines (43 loc) · 1.22 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
#include <iostream>
#include <string>
#include <math.h> /* floor */
/*
* Palindrome checker
*
* Write code to check if a string is palindrome or not?
*
* A string is a palindrome if the string is the same
* whether spelled forwards or backwards
*/
std::string isPalindrome(std::string text) {
// remove non alphabetic characters
std::string newText;
for (int i = 0; i < text.size(); i++) {
if (std::isalpha(text[i])) {
newText += std::tolower(text[i]);
}
}
// check if a string is a palindrome.
// Only check half of the array instead of the whole string
for (int i = 0; i < floor(newText.size() / 2) + 1; i++) {
if (newText[i] != newText[newText.size() - i - 1]) {
return "False";
}
}
return "True";
}
void test(std::string text) {
std::cout << text << ": " << isPalindrome(text) << std::endl;
}
int main(int argc, char *argv[]) {
std::cout << "palindrome tests" << std::endl;
test("racecar");
test("r a c e c a r!");
test("Mother Eve's noose we soon sever, eh Tom?");
test("On a clover, if alive, erupts a vast, pure evil; a fire volcano.");
std::cout << "Non-palindrome tests" << std::endl;
test("abcd");
test("palindrome");
test("!!hacktoberfest!!");
return 0;
}