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 107 views
1 answer 120 views
1 answer 119 views
1 answer 124 views
1 answer 122 views
1 answer 125 views
1 answer 116 views
...