❓ Help TEST BOT

Sendlove

𝒜 𝓢𝓱𝓲𝓷𝓲𝓷𝓰 𝓑𝓮𝓪𝓬𝓸𝓷, 𝓔𝓶𝓹𝓪𝓽𝓱
Elite
Create a c++ program that accepts a string input and checks if the string is a palindrome or not.
 
Certainly! Here's a C++ program that checks if a given string is a palindrome or not:

C++:
#include <iostream>
#include <string>
#include <algorithm>

bool isPalindrome(const std::string& str) {
    std::string temp = str;
    std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
    std::string reversed = temp;
    std::reverse(reversed.begin(), reversed.end());
    return temp == reversed;
}

int main() {
    std::string input;
    std::cout << "Enter a string: ";
    std::getline(std::cin, input);

    if (isPalindrome(input)) {
        std::cout << input << " is a palindrome." << std::endl;
    } else {
        std::cout << input << " is not a palindrome." << std::endl;
    }

    return 0;
}

In this program, the isPalindrome() function checks if a given string is a palindrome or not. It creates a copy of the input string, converts it to lowercase using std::transform, creates a reversed copy of the lowercase string using std::reverse, and finally checks if the original lowercase string and the reversed lowercase string are equal.

In the main() function, it prompts the user to enter a string, stores it in the input variable, and then calls isPalindrome() to determine if the input string is a palindrome or not. Finally, it outputs the result to the console.
 

About this Thread

  • 1
    Replies
  • 256
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
937
Guests online
1,004
Total visitors
1,941

Forum statistics

Threads
2,276,293
Posts
28,968,990
Members
1,231,205
Latest member
SPAdma
Back
Top