I'm making this program to verify if a entered data is palindrome or not, something is wrong with it

If I type 'ab' or 'abcba', it says that it is palindrome, while it is not. Also if i type 'try', it says it is not palindrome, which is true. Please fix the error.

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
#include <iostream>
using namespace std;
int main()
{
	const char null_char='\0';
	char str[100];
	int length=0;
	bool smart=true;
	
	cin>>str;
	
	for( ; str[length]!=null_char ; length++);
	
	for(int lside=0,rside=length-1 ; lside<length/2,rside>length/2 ; lside++,rside--)
	{
		if((str[lside]) == (str[rside]))
		{
			true;
		}
		else
		{
			smart=false;
			break;
		}
	}	
	if(smart == true)
	{
		cout<<"It is palindrome";
	}
	else
	{
		cout<<"It is not palindrome";
	}
	return 0;
}
Last edited on
lside<length/2 is ignored due to the comma operator.

ab ->

length -> 2
length/2 -> 1

rside=length-1 -> 1

-> Due to rside>length/2 no iteration.

abcba is actually a palindrom (due to rside>length/2 'c' is not considered)
I worked on different solution.

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>

using namespace std;

int main() {

    // assume we have a palindrome
    bool palindrome = true;


    string str; //forward string var
    string rstr; //backward string var


    // input
    getline(cin, str);
    // copy value
    rstr = str;

    // reverse value
    std::reverse(rstr.begin(), rstr.end(),);


    int index = 0;
    for (char const &c: str) {

        // compare string with string reversed char by char including spaces.
        if (c != rstr[index++]) {
            palindrome = false;
        }

    }

    // print out answer
    if (palindrome) {
        cout << str << " is a palindrome" << endl;
    } else {
        cout << str << " is not  a palindrome" << endl;

    }

    return 0;
}
I suspect it is something to do with that tricky, difficult-to-read line 14. The second statement in a for declaration specifies a check to perform to decide whether or not to continue the loop. The only check here that matters is the rside > length/2 because it falls on the right side of the comma operator. I think changing the greater-than sign to greater-than-or-equal-to sign may fix your problem, but it is a superfluous check.

To make the code clearer and easier to debug, use the for loop structure to move and evaluate lside. Inside the loop, just calculate rside each time. It will be a lot easier to read the code and see what's going on.
1
2
3
4
5
6
7
8
9
10
bool isPalindrome = true;
    for(int lside = 0; lside < length/ 2; ++lside)
    {
        int rside = (length - 1) - lside;
        if(str[lside] != str[rside])
        {
            isPalindrome = false;
            break;
        }
    }
@Bdanielz I haven't studied strings yet. Thanks for your help though.
And @coder777;
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
#include <iostream>
using namespace std;
int main()
{
	const char null_char='\0';
	char str[100];
	int length=0;
	bool smart=true;
	
	cin>>str;
	
	for( ; str[length]!=null_char ; length++);
	
	for(int lside=0,rside=length-1 ; lside<(length/2) && rside>=(length/2) ; lside++,rside--)
	{
		if((str[lside]) == (str[rside]))
		{
			true;
		}
		else
		{
			smart=false;
			break;
		}
	}	
	if(smart == true)
	{
		cout<<"It is palindrome";
	}
	else
	{
		cout<<"It is not palindrome";
	}
	return 0;
}

It's working now
Topic archived. No new replies allowed.