Palindrome

Pages: 12
Please keep in mind I'm a pure beginner.

We deal with beginners all the time.
You require a "special needs" class.
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. 

Don't be rude and disrespectful saying i need a special needs class.
This is what i have now but again i don't know what I'm doing

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
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()
{
    
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])
			return false;


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;
}
https://www.daniweb.com/programming/software-development/threads/356401/getting-deeper-into-trouble

How time flies - you've been working on this for 8 years! Much longer and your special needs will include a Zimmer frame.
dont be rude and that wasnt me 8 years ago
that wasnt me 8 years ago
C'mon Zimmermann even you can do better than that as a comeback.
can someone help me fix this code

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
#include <string>
#include <iostream>
using namespace std;
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 std::string &strg ) : std::string( strg ) 
    
    {

	int counter = strg.size();
	if (strg[0] =! strg[counter])
	return false;
	else
	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;
}
 
He is so obviously a troll.
Wake up people.
how am i a troll ? im asking for help. I dont sit on here and harass people all day like you. im seeking help
Fuck off, troll.
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
70
71
72
73
74
75
76
#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-fns
        bool isPalindrome() const ;
};

bool Pstring::isPalindrome() const
{
    // strings of zero or one characters are palindromes
    if( size() < 2 ) return true ;

    // 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/tolower
        const unsigned char l = operator[](left) ;
        const unsigned char r = operator[](right) ;

        // if they do not compare equal (case insensitive); it is not a palindrome
        if( std::tolower(l) != std::tolower(r) ) return false ;

        // equal, now check the next two chars
        ++left ;
        --right ;

        // this loop is repeated till left and right meet or cross each other
    }

    return true ; // 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";
}
Hello

Thank you. This helps me a lot because it list comments for each section and step you're doing.
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])
			return false;
Topic archived. No new replies allowed.
Pages: 12