Question

In: Computer Science

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

In 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 to in your implementation.

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.

Solutions

Expert Solution

code:

#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<cctype>
using namespace std;
bool isPalindrome(string s)
{
stack<char> st;
queue<char> qu;
for(int i=0;i<s.length();i++)//looping over each character of the string
{
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')||(s[i]>='0'&&s[i]<='9'))//pushing only alphanumeric characters
{
//tolower is used to handle all possible usecases, as any character will be converted to lower case
st.push(tolower(s[i]));//pushing data to stack, which gives string in reverse order when popped
qu.push(tolower(s[i]));//pushing data to queue, which gives string in same order when popped
}
}
while(!st.empty()&&!qu.empty()&&st.top()==qu.front())//checking for palindrome
{
st.pop();//removing the seen elements from stack
qu.pop();//removing the seen elements from queue
}
if(st.empty()&&qu.empty())//If queue and stack is empty then it means the string is a palindrome
{
return true;
}
return false;
}
int main()
{
string s;
getline(cin,s);//read a complete string
if(isPalindrome(s))
cout<<"true";
else
cout<<"false";
}

Sample i/o:

Explanation:

The sample i/o is presented here and the explanation for each line is presented in the code. STL is used for implementation of the queue and stack

**please upvote if you like the answer


Related Solutions

c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
using recursion no loops allowed(for,while..ect) not allowed #4: Write a recursive function ‘bool palindrome(string s)’ that...
using recursion no loops allowed(for,while..ect) not allowed #4: Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #6: Write a function 'string binary(int n)' that returns a string of 1's and 0's which is the binary representation of n. cout << binary(43) <<...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
C++ function to a string into enum function to a enum into a string check valid...
C++ function to a string into enum function to a enum into a string check valid / loop ( asking a couple of times until answer invaild) For example Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY) output should be what is your favorit fruit? : strawberry you will have STRAWBERRY. what is your favorite fruite : peach invaild TIA
Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should...
Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should be case-independent. * @param word a String without whitespace * @return true if the word is a palindrome    Public static int longestWordLength(String words) * Returns length of the longest word in the given String using recursion (no loops). * Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, * use the following method on your String to remove...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
This C++ assignment asks to write a function that determines if a C-string begins with a...
This C++ assignment asks to write a function that determines if a C-string begins with a specified prefix. It should have the following signature: bool starts(char *str, char *prefix) It should return true if str begins with prefix, false if not. It should return false if prefix is longer than str. See the table below for some examples of what your function should return for various cases: str prefix returns airplanes air true airplanes abc false airplanes plane false airplanes...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT