forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome.cpp
54 lines (44 loc) · 1.33 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
48
49
50
51
52
53
54
/*
isPalindrome Algorithm in C++
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
*/
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s) {
// Step 1: Convert all characters to lowercase
for (char& c : s) {
c = std::tolower(c);
}
// Step 2: Remove non-alphanumeric characters
std::string alphanumericStr;
for (char c : s) {
if (std::isalnum(c)) {
alphanumericStr.push_back(c);
}
}
// Step 3: Check if the resulting string is a palindrome
int left = 0;
int right = alphanumericStr.length() - 1;
while (left < right) {
if (alphanumericStr[left] != alphanumericStr[right]) {
return false;
}
left++;
right--;
}
return true;
}
int main() {
std::string input;
std::cout << "Enter a string: ";
std::getline (std::cin, input);
if (isPalindrome(input)) {
std::cout << "The string is a palindrome." << std::endl;
}
else {
std::cout << "The string is not a palindrome." << std::endl;
}
return 0;
}