How to check whether a sentence is palindrome in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <algorithm>

bool isSentencePalindrome(std::string str) {
    // Change the string into lowercase and remove all non-alphanumeric characters
    std::transform(str.begin(), str.end(), str.begin(), [](unsigned char ch){ return std::tolower(ch); });
    str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char ch){ return !std::isalnum(ch); }), str.end());

    return str == std::string(str.rbegin(), str.rend());
}

int main() {
    std::cout << (isSentencePalindrome("Top step's pup's pet spot.") ? "yes" : "no") << std::endl;
}

 
 
 
/*
run:
 
yes
 
*/

 



answered Jul 1, 2024 by avibootz

Related questions

1 answer 113 views
1 answer 128 views
1 answer 127 views
1 answer 133 views
1 answer 131 views
1 answer 131 views
1 answer 126 views
...