A Palindrome is a word that reads the same backwards as it does when being read forwards, I want to show you how to make a program that checks to see if a word is a Palindrome using C++, It can serve as a good challenge for beginners like myself, it is usually given when learning about the STL so i will use some STL components. First I will make the Palindrome program using strings
then deques
:
//PALINDROME USING STRINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using std::cout;
using std::endl;
bool Palindrome(std::string &s){
std::string take;
for(auto &a: s){ // CHECKS IF THERE ARE ANY NON ALPHABET CHARACTERS IN THE WORD
if(std::isalpha(a))
take.push_back(a);
}
std::string collect{take};
std::reverse(collect.begin(), collect.end()); //MAKES THE CONTENT OF 'COLLECT' REVERSED
if(collect == take)
return true;
else
return false;
}
int main(){
std::string temp;
cout << "Enter the word: ";
std::cin >> temp;
std::transform(temp.begin(), temp.end(), temp.begin(), ::toupper); //MAKES THE WORD UPPERCASE
std::cout << std::boolalpha;
std::cout << std::setw(8) << std::left << "Result" << "String" << std::endl;
std::cout << std::setw(8) << std::left << Palindrome(temp) << temp << std::endl;
std::cout << std::endl;
}
Ok so let me walk through what's going on in the bool Palindrome function
Note: I called the
transform
algorithm inmain
to make the words uppercase, it's not needed, i just wanted the words to be in CAPS.
So I create a string take
and then loop through s
to check if there are any non-alphabet characters in it then push it into take
, we then create a string collect
and initialize it to the value of the string take
which contains the word that we are checking to see if it's a palindrome, then rather than start using unnecessary loops i just call the std::reverse()
algorithm and pass in an iterator to the beginning and end of the string collect
,
Note: The
std::reverse()
algorithm reverses the order of elements in a range
then I use an if
statement to check if collect
is still the same as take
after I've reversed it, either true
or false
is returned based on the outcome.
Lets go to main()
, I ask the user for the a word then use transform
to make it uppercase, I've formatted my output to make it look how i want it using std::setw
and std::left
which are from the iomanip
header, The Palindrome function is called and the user inputs a word and sees if it's a palindrome or not
USING A DEQUE
I will recreate the Palindrome program using a deque
instead of a string
, this is what it would look like:
//PALINDROME USING DEQUES
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
bool is_palindrome(std::string& s)
{
std::deque<char> dek;
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
for(auto &a:s){
if(std::isalpha(a))
dek.push_back(a);
}
std::deque<char> kek = dek;
std::reverse(kek.begin(), kek.end());
if(dek == kek)
return true;
else
return false;
}
int main()
{
std::vector<std::string> test_strings;
std::string temp;
std::cout << "Enter the word: ";
std::cin >> temp;
test_strings.push_back(temp);
std::cout << std::boolalpha;
std::cout << std::setw(8) << std::left << "Result" << "String" << std::endl;
for(auto& s : test_strings) {
std::cout << std::setw(8) << std::left << is_palindrome(s) << s << std::endl;
}
std::cout << std::endl;
return 0;
}
I use a deque<char>
to model a string, then I make the word passed into the function uppercase and loop through the word and check if each individual character in the word is an alphabet(in case the user inputted a comma or something else) then push it into the deque
if it's an alphabet. I create another deque
and initialize it with the value of the former deque
which now contains the word passed into the function, then I use the reverse
algorithm to reverse the order of characters in the new deque
, I then compare both deques
, if they're the same then true is returned which indicates that the word is a Palindrome. That's how a Palindrome program can be created using strings
or deques
, you could also use stack
and queue
to do it.