0% found this document useful (0 votes)
6 views3 pages

Palindrome c9

Uploaded by

chetanaingle7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Palindrome c9

Uploaded by

chetanaingle7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <stack>

#include <cctype>

using namespace std;

// Function to print original string followed by reversed string using a stack

void printOriginalAndReversed(const string& input) {

stack<char> charStack;

// Push characters onto the stack

for (char ch : input) {

charStack.push(ch);

// Print original string

cout << "Original String: " << input << endl;

// Print reversed string

cout << "Reversed String: ";

while (!charStack.empty()) {

cout << charStack.top();

charStack.pop();

cout << endl;


}

// Function to check whether a given string is a palindrome or not

bool isPalindrome(const string& input) {

stack<char> charStack;

// Push characters onto the stack

for (char ch : input) {

charStack.push(ch);

// Compare characters from the stack with the original string

for (char ch : input) {

if (charStack.top() != ch) {

return false;

charStack.pop();

return true;

int main() {

string input;
// Input from the user

cout << "Enter a string: ";

getline(cin, input);

// Remove punctuation, convert to lowercase, and remove spaces

string cleanInput;

for (char ch : input) {

if (isalnum(ch)) {

cleanInput += tolower(ch);

// a) Print original string followed by reversed string using a stack

printOriginalAndReversed(cleanInput);

// b) Check whether the given string is a palindrome or not

if (isPalindrome(cleanInput)) {

cout << "The string is a palindrome." << endl;

} else {

cout << "The string is not a palindrome." << endl;

return 0;

You might also like