Question

Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....

Using c++

Valid Palindrome

In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: for the purpose of this problem, we define empty string as valid palindrome.

Example 1: Input: ”A man, a plan, a canal: Panama” Output: true

Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is a palindrome or not. In this assignment, you need to use stack AND queue.

Suggestions: You don’t need to create the stack and queue classes by yourself. You can use the stack and queue implemention provided on Blackboard or use STL directly. Consider all possible cases

Homework Answers

Answer #1
#include<iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
#define MAX 50

class Stack  {
private:
  char data[MAX],str[MAX];
  int top,length,count;

  void pushData(char);
  char popData();

public:
  Stack() {
    top=-1;
    length=0;
    count=0;
  }
  void getString();
  void checkPalindrome();
  void extractString();
  void displayReverse();
};

int main()  {
  Stack obj;
  obj.getString();
  obj.extractString();
  cout<<"\n String extracted after removing punctuations and symbols";
  cout<<"\n String converted to lower case";
  cout<<"\n Reverse of entered string: ";
  obj.displayReverse();
  obj.checkPalindrome();
  return 0;
}

void Stack::getString() {
  cout<<"\n Enter a String: ";
  cin.getline(str,MAX);

  length=strlen(str);
}

void Stack::extractString() {
  char temp[MAX];
  int i,j;
  for(i=0; i<length; i++) {
    temp[i]=str[i];
  }
  j=0;
  for(i=0; i<length; i++ )  {
    if(isalpha(temp[i]))  {
      str[j]=tolower(temp[i]);
      j++;
    }
  }

  length=j;   //update length with new str length

}

void Stack::checkPalindrome() {
  for(int i=0; i<length; i++)
    pushData(str[i]);

  for(int i=0; i<length; i++) {
    if(str[i]==popData())
      count++;
  }

  if(count==length) {
    cout<<"\n true\n";
  }
  else  cout<<"\n false \n";
}

void Stack::displayReverse()  {
  for(int i=length-1; i>=0; i--)
    cout<<str[i];
}

void Stack::pushData(char temp) {

  if(top==MAX-1)  {
    cout<<"\n Stack Overflow!!!";
    return;
  }

  top++;
  data[top]=temp;

}

char Stack::popData() {
  if(top==-1)  {
    cout<<"\n Stack Underflow!!!";
    char ch='\n';
    return ch;
  }
  char temp=data[top];
  top--;
  return temp;
}
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
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...