-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathpalindrome.cpp
47 lines (40 loc) · 1.24 KB
/
palindrome.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
#include <iostream>
#include <algorithm>
#include <cctype>
bool is_palindrome(std::string s) {
s.erase(std::remove_if(s.begin(), s.end(), [](char c) {
return !std::isalnum(c);
}), s.end());
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
std::string reversed(s.rbegin(), s.rend());
return s == reversed;
}
int main() {
std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
if (is_palindrome(str)) {
std::cout << "'" << str << "' is a palindrome." << std::endl;
} else {
std::cout << "'" << str << "' is not a palindrome." << std::endl;
}
return 0;
}
/-----------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <cctype>
bool is_palindrome(std::string s) {
s.erase(std::remove_if(s.begin(), s.end(), [](char c) {
return !std::isalnum(c);
}), s.end());
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
std::string reversed(s.rbegin(), s.rend());
return s == reversed;
}
int main() {
std::string str = "A man, a plan, a canal: Panama";
std::cout << (is_palindrome(str) ? "True" : "False") << std::endl;
return 0;
}
// Output: True