I need help with an assignment. Please keep in mind I'm a pure beginner. I really don't know much beyond basic cin and cout. So please keep that in mind. I have attached what I started, but I'm unsure what to do going forward.
For this assignment, you will create an Object-Oriented C++ program that tests for palindromes. A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes.
Write a class Pstring that is derived from the STL string class. The Pstring class adds a member function
bool isPalindrome()
that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main program that asks the user to enter a string. The program uses the string to initialize a Pstring object and then calls isPalindrome() to determine whether the string entered is a palindrome. Be sure to including comments throughout your code where appropriate.
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.
The assignment isn't quite right, because ispalindrome() is a boolean function just like you might imagine isspace(char) to be. However, we'll work with what you need to get done.
Your class is pretty much correct. The constructor needs to initialize the std::string base class.
1 2 3 4
Pstring::Pstring(std::string word)
: std::string(word) // actually, it's more correct to do this: std::string(std::move(word))
{
}
The next part is the palindrome function. It looks like:
1 2
bool Pstring::ispalindrome() {
}
So, what goes in there? Do you know what a palindrome is? How would you check if these words are palindromes?
It would better design for Pstring::ispalindrome to be a non-member function, because it can be implemented without knowledge of the internal representation of the class. (Regardless, follow the assignment's instructions.)
string has a reverse. Its less efficient than the fancy magic above but compare reverse and normal, possibly with spaces removed and case normalized first, and you have your result.
or you can brute force it and compare the last and first letter, then go towards the middle by one, over and over until you get to the middle. This should be a loop that you can do even if you are very new to coding?
The algorithm (to decide whether a string is a palindrome) works like this:
1. compare the first and last characters of the string
If the characters differ, the string is not a palindrome.
2. compare the second and second-to-last characters of the string
If the characters differ, the string is not a palindrome.
3. compare the third and third-to-last characters of the string
If the characters differ, the string is not a palindrome.
Repeat until you meet in the middle. Once you've met in the middle, you can say the string is a palindrome.
For example, with the string "abcba":
Compare the first and last characters:
a b c b a
^ ^
Compare the second and second-to-last characters:
a b c b a
^ ^
Compare the third-and third-to-last characters:
a b c b a
^
^
We've met in the middle, we can conclude the string is a palindrome.
The code I've posted uses the C++ standard library to express this idea.
It can be done with a for-loop containing an if-statement.
If cout is the first name of an object, std is its surname. So std::cout is just the full or legal name of cout. It's similar for everything else with a std:: in front of it.
Can someone point out my mistake
Your new assignment is to write the function bool is_palindrome(std::string s)
Once that is complete you can write your class Pstring to work together with it.
Pstring is supposed to be derived from std::string. Your Pstring contains a std::string object.
As for ispalindrome(), do it like this:
for i = 0 to half the size of the string
if 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.
So what is the "corresponding character at the end of the string?? You'll need to figure out the math for this.
Character 0 corresponds to the last character
character 1 corresponds to the next-to-last character
etc.
#include <string>
#include <iostream>
class PString : public std::string
{
public:
PString( const std::string &aString );
bool isPalindrome() const;
};
PString::PString( const std::string &aString ) : std::string( aString )
{
}
bool PString::isPalindrome() const
{
// You can fill in the logic here:
returntrue;
}
int main()
{
std::string str;
std::cout << "This is a palindrome-testing program. Enter a string to test:\n";
std::cin >> str;
// Create a PString object that will check strings
PString s(str);
// Check string and print output
if (s.isPalindrome())
{
std::cout << s << " is a palindrome";
}
else
{
std::cout << s << " is not a palindrome";
}
std::cout << std::endl;
return 0;
}
you just return true for all inputs in that code. so yes, it will say anything and everything is a palindrome. Do you see that block of code looping to check for anything? doing anything at all? It even has a comment: put some code here!!!!! All the stuff ppl above been saying needs to be inserted.