You have all the basic stuff for the program, now you just need to write the code that actually checks for a palindrome. That code repllaces the "return true;" at line 29.
See if you can convert the pseudo code I gave earlier into C++. Here it is again:
1 2 3 4 5 6 7 8
for i = 0 to half the size of the string {
// Compare the ith character at the front of the string
// to the corresponding character at the end of string
// if the two are different, return false
}
// If you get to the end of the loop then each character at the front
// is equal to the corresponding character at the end. So it's a palindrome
// and you can return true.
#include <iostream>
#include <string>
#include <cctype>
// Write a class Pstring that is derived from the STL string class
class Pstring : public std::string
{
public:
// Include a constructor that takes an STL string object as parameter
// and passes it to the string base class constructor.
/* explicit */ Pstring( const std::string& str ) : std::string(str) {}
// The Pstring class adds a member function bool isPalindrome()
// we make it const correct
// https://isocpp.org/wiki/faq/const-correctness#const-member-fnsbool isPalindrome() const ;
};
bool Pstring::isPalindrome() const
{
// strings of zero or one characters are palindromes
if( size() < 2 ) returntrue ;
// A palindrome is a string that reads the same backward as forward.
// we need to check if this is so
// we use mbozzi's algorithm, from this post
// http://www.cplusplus.com/forum/beginner/267390/#msg1150470
// std::size_t unsigned integer type, often used for array indexing
// https://en.cppreference.com/w/cpp/types/size_t
std::size_t left = 0 ; // position of the char at the left (initially left most)
std::size_t right = size() - 1 ; // position of the char at the right (initially right most)
// note that if the string has N characters, the right most char is at position N-1
// You may find it useful to use the subscript operator [] of the string class:
// if str is a string object and k is an integer, then str[k] returns the
// character at position k in the string.
while( left <= right )
{
// TO DO: ideally, skip non-alpha numeric characters
// check if the char on the left is equal (case insensitive) to the one on the right
// unsigned char because std::tolower "To use these functions safely with plain chars,
// the argument should first be converted to unsigned char"
// https://en.cppreference.com/w/cpp/string/byte/tolowerconstunsignedchar l = operator[](left) ;
constunsignedchar r = operator[](right) ;
// if they do not compare equal (case insensitive); it is not a palindrome
if( std::tolower(l) != std::tolower(r) ) returnfalse ;
// equal, now check the next two chars
++left ;
--right ;
// this loop is repeated till left and right meet or cross each other
}
returntrue ; // all chars were matched; so it is a palindrome
}
int main()
{
std::string str;
std::cout << "This is a palindrome-testing program. Enter a string to test: ";
std::cin >> str;
// Create a PString object that will check strings
const Pstring s(str);
// Check string and print output
if (s.isPalindrome()) std::cout << s << " is a palindrome\n";
else std::cout << s << " is not a palindrome\n";
}
You're getting very close. Explain what this code does. As you explain it, I think you'll notice that some parts are incorrect:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
bool Pstring::ispalindrome()
{
for (int i = 0; i < str.size(); i++)
if(islower(str[i]))
str[i] = toupper(str[i]);
return str;
}
bool checkPalindrome(string str)
{
int n = str.size();
for (int i=0; i<str.size()/2;i++)
if (str[i] !=str[n-1-i])
returnfalse;