How to determine palindrome strings in C++?

Here is a C++ program to determine if a string is a palindrome.

#include <iostream>
#include <string>
#include <algorithm>

bool isPalindrome(std::string str) {
    std::string reversedStr = str;
    std::reverse(reversedStr.begin(), reversedStr.end());
    
    return str == reversedStr;
}

int main() {
    std::string str;
    std::cout << "Enter a string: ";
    std::cin >> str;
    
    if (isPalindrome(str)) {
        std::cout << str << " is a palindrome." << std::endl;
    } else {
        std::cout << str << " is not a palindrome." << std::endl;
    }
    
    return 0;
}

The program first defines a function called isPalindrome, which takes a string as a parameter and returns a boolean value to indicate whether the string is a palindrome. In the main function, the user inputs a string, then calls the isPalindrome function to determine if it is a palindrome and outputs the corresponding result.

After running the program, input a string and the program will output whether the string is a palindrome.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds