Palindrome

Pages: 12
Hello All,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
#include <string>
#include <vector>
using namespace std;

class Pstring : public string
{
public:
	Pstring(string);
	bool ispalindrom();

};

Pstring::Pstring(string word)
{

}
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?
elephant
kayak
Last edited on
do you mean like this ? palindrome reads things backwards. kayak is a palindrome. elephant is not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iostream>
#include <string>
#include <vector>
using namespace std;

class Pstring : public string
{
public:
	Pstring(string);
	bool ispalindrom();

};

bool Pstring:ispalindrome() {

    
}

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.)

1
2
bool is_palindrome(std::string_view s) 
{ return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin()); }
http://coliru.stacked-crooked.com/a/5b61b8ecaf8cf586
Last edited on
Not to be derisive, but it's clearly just an assignment, so the professor abusing OOP is out of OP's hands.
Sure. Of course OP needs to follow the instructions.
mbozzi

im confused. thats wat advanced past my knowledge of c+
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?
Last edited on
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.
Last edited on
i know how to do a very basic loop with simple conditonal statemnet (i > 1). any other conditional statment i dont know how to do
so what do i need to do what this code ? PS: i dont know how to use std, i only use cout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include <iostream>
#include <string>
#include <vector>
using namespace std;

class Pstring : public string
{
public:
	Pstring(string);
	bool ispalindrom();

};

bool Pstring:ispalindrome() {

    
}
This is partial code that is patched together. Can someone point out my mistake and walk me through it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
include <iostream>
#include <string>
#include <vector>
using namespace std;

class Pstring 
{
public:
 string string_value;
 public:
 Pstring(string str);
 bool ispalindrome();
};

Pstring::Pstring(string str)

{
    string_value=str
}

bool Pstring::ispalindrome()
{
    
int n = string_value.size();





int main () {
    string str;
    cout <<"Please enter a word";
    cin >> str;
    
    Pstrings(str);
    if(s.ispalindrome())
    
    cout << str << "is a palindrome " << endl;
    
    else
    
    cout << str << "is a palindrome " << endl;
}
I dont know how to use std
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.
Last edited on
Can someone point out my mistake

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.

Hope this helps.
Can you tell me what line i need to making these changes at ?
This code is coming back with incorrect results. It's telling me its a pallindrome when its not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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:

	return true;

}

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;

}
1
2
3
4
bool PString::isPalindrome() const
{
	return true;  //ya think?
}


What are you trying to say
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.
To be quite blunt, if you don't understand why that yields incorrect results, you're missing fundamentals and need to study the very basics.
Last edited on
Pages: 12