Question

Write a program in c++ that uses a stack to test whether a given string is...

Write a program in c++ that uses a stack to test whether a given string is a palindrome.

Homework Answers

Answer #1
#include<iostream>
#include<stack>

using namespace std;

bool isPalindrome(string candidate)
{
   stack<char> s;
   for(int i = 0;i<candidate.length();i++){
      if(islower(candidate[i]) || isdigit(candidate[i])){
         s.push(candidate[i]);
      }
   }

   int k = 0;
   while(!s.empty()){
      if(s.top() != candidate[k++]){
         return false;
      }
      s.pop();
   }
   return true;
}

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that determines whether an input string a palindrome; that is whether it can...
Write a program that determines whether an input string a palindrome; that is whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly in a stack implementation). Consider using multiple stacks. PLEASE PROVIDE A PSEUDOCODE
Write a Python program that reads in a string and determines whether the string is a...
Write a Python program that reads in a string and determines whether the string is a palindrome or not. note that a sentence that reads the same forwards and backwards by resequencing the spaces is also a palindrome and your program should consider this. sample run enter string: mom output: mom is a polindrame enter string: a nut for a jar of tuna output: a nut for a jar of tuna is a palindrome
Write a program in C that extracts the tokens from a string. Given a string as...
Write a program in C that extracts the tokens from a string. Given a string as input, the program should print on the screen each token on a new line. For example given the following string as input: “hello there my friends” the program should generate: I am a programmer
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
Write an algorithm not code or solution only algorithm such as (step1, step2.....about. 1.How would you...
Write an algorithm not code or solution only algorithm such as (step1, step2.....about. 1.How would you use a stack to verify whether a given string is a palindrome or not. 2. How would you check if a string is a palindrome or not, using queues and deques.
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
C++ program checks if an input string is a palindrome or not. Ignore case and remove...
C++ program checks if an input string is a palindrome or not. Ignore case and remove all non-alphanumeric characters in the input string. C++ program uses only one string (no extra-string or array). And also, use two indices to point from the beginning and ending positions. Sample Input 0 Race car Sample Output 0 TRUE Sample Input 1 7...8 Don't nod 78. Sample Output 1 FALSE
C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks...
C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks to see if the top starts with ‘A’ or ‘a’. If so, duplicate the top of the stack (i.e. pop a copy of that value onto the stack) else if length > 10, pop it off the stack