Challenge: Palindrome Sentence
Completed
Read a whole line. Ignoring spaces, punctuation and capital letters, is it a palindrome? Use two pointers that skip anything that isn't a letter or digit. Print yes or no.
Input
One line of text.
Output
yes or no.
Example
Input
A man, a plan, a canal: Panama
Output
yes
Editor
C++
Hints
getline(cin, s) reads the whole line, spaces included.
isalnum() checks letters/digits; tolower() ignores case.
Solution explanation
One pass with two pointers: O(n) time, O(1) space.
Show solution
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
int l = 0, r = (int)s.size() - 1;
bool ok = true;
while (l < r) {
if (!isalnum((unsigned char)s[l])) l++;
else if (!isalnum((unsigned char)s[r])) r--;
else if (tolower((unsigned char)s[l]) != tolower((unsigned char)s[r])) { ok = false; break; }
else { l++; r--; }
}
cout << (ok ? "yes" : "no") << endl;
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module