Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A palindrome is a word or phrase that may be read the same way in either directi

ID: 3771424 • Letter: A

Question

A palindrome is a word or phrase that may be read the same way in either direction. Some examples of words that are palindromes are: “mom”, “radar”, “kayak”, “rotor”, and “racecar”. The word “mos” is not a palindrome. Examples of phrases that are palindromes are: “a man a plan a canal panama”, “able was i ere i saw elba”, “never odd or even”, and “was it a car or a cat i saw”. Write a C++ FUNCTION that determines if a C++ vector of characters is a palindrome. The function has a single parameter, a C++ vector of type char, and returns a value of type bool. It returns true only if the input parameter is a palindrome. You may assume that the characters in the input C++ vector will only contain lowercase alphabetic characters and spaces. Hint: Copy all of the non-space (i.e., alphabetic) characters from the input vector into a new vector and then make a reverse copy of the new vector. Compare these two vectors to see if they are the same. Your solution must solve this problem using C++ vector(s) and not using C++ strings.

HERE IS THE SOLUTION:

bool validPalindrome(const vector<char> & phrase)
{
vector<char> cleanPhrase;
for (int i = 0; i < phrase.size(); i++)
{
if (phrase[i] != ‘ ‘)
{
cleanPhrase.push_back(phrase[i]);
}
}
vector<char> reverse;
for (int i = cleanPhrase.size() – 1; i >= 0; i--)
{
reverse.push_back(cleanPhrase[i]);
}
bool valid = true;
int i = 0;
while (valid && i < cleanPhrase.size())
{
if (cleanPhrase[i] != reverse[i])
{
valid = false;
}
i++;
}
return valid;
}

can someone just break it down and explain it to me? thanks

Explanation / Answer

/*
In the question its given a hint to solve this problem.
the hint is that we use another vector to store the content of the
original array but in reverse manner suppose original string is "chegg"
then the reverse will be "ggehc" now since both these are not equal hence
"chegg" is not palindrome. For instance string "madam", its reverse is
"madam" itself hence it is a palindrome.
*/
//here return value is bool and original vector is denoted by phrase
bool validPalindrome(const vector<char> & phrase)
{
vector<char> cleanPhrase; //this vector is used to store original string

//this is done just to store the original array without spaces
//because while considering a string to be palindrome we do not consider
//spaces
/*
This is simply a iterator loop which starts from the index 0
i.e. start point of the vector phrase and go all the way upto the end
i.e. upto phrase.size() I have assumed that you know the basic functionality
of loop. If not then please comment on the thread
*/
for (int i = 0; i < phrase.size(); i++)
{
if (phrase[i] != ' ') //if the character is not a space then insert it into cleanPhrase
{
cleanPhrase.push_back(phrase[i]); //push_back is a way of inserting character into vector from front
}
}

vector<char> reverse; //this vector is used to store reverse string

/*
The idea is to start at the back of the vector and go to front
hence here we start at the back of the vector.
Index of the last character of cleanPhrase vector is cleanPhrase.size() - 1
and we go all upto the front hence i>=0
*/
for (int i = cleanPhrase.size() - 1; i >= 0; i--)
{
reverse.push_back(cleanPhrase[i]); //insert all the character from cleanPhrase into reverse vector
}
bool valid = true; //here we assumed that cleanPhrase and reverse vector are equal
int i = 0;

/*
Here we are comparing both original and reverse vector
If they turned out to be same them BOOM the original one was
palindromic if not then sadly it was non-palindromic
*/
while (valid && i < cleanPhrase.size())
{
//here we are going to every character element of cleanPhrase vector one by one
if (cleanPhrase[i] != reverse[i]) //checking every character of both vector for equality
{
valid = false; //if any character is not equal then BOOM string is not palindromic
}
i++;
}
return valid;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote