4 Replies - 678 Views - Last Post: 30 August 2019 - 04:56 AM Rate Topic: -----

#1 SC0TTii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-August 19

Palindrome Help

Posted 25 August 2019 - 12:29 PM

So in most cases my palindrome works, but sometimes it doesn't. For example, "racecar" works as a palindrome, and when I change the last letter to and 'e', "racecae", it doesn't work. But the problem is when I change one of the letters on the inside... and I don't understand what I did wrong. All help is appreciated!"

#include <iostream>
using namespace std;

void checkIfPalindrome(string input);


int main() {

	//PALINDROME
	checkIfPalindrome("dadadad");

	return 0;
}

void checkIfPalindrome(string input) {

	bool isPalindrome = false;

	for (int i = 0; i < input.length(); i++) {

		for (int j = input.length() - 1; j >= 0; j--) {

			if (input[i] == input[j]) {

				isPalindrome = true;

			}
			else
			{
				isPalindrome = false;
			}

		}

	}

	if (isPalindrome) {
		cout << "Palindrome." << endl;
	}
	else {
		cout << "Not a palindrome." << endl;
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome Help

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15337
  • View blog
  • Posts: 61,463
  • Joined: 12-June 08

Re: Palindrome Help

Posted 25 August 2019 - 01:25 PM

I would suggest exiting your for loops when you find something is not a match.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • Joined: 21-June 11

Re: Palindrome Help

Posted 25 August 2019 - 01:28 PM

I see two things wrong conceptually:

1. The nested loops: For each i, you iterate over multiple js, meaning that you compare each character in the string to multiple other characters. But in a palindrome each character only has a single corresponding character that needs to be checked.
2. The isPalindrome variable can change multiple times. So at one point you might decide that the string is not a palindrome because two corresponding letters don't match. So you set it to false. But then later you may set it back to true again because two other characters match. So you only remember your last decision. But that's not how it should work. Once you've found characters that don't match, that's it. The result should be false and you shouldn't be able to change it back to true again later.
Was This Post Helpful? 1
  • +
  • -

#4 SC0TTii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-August 19

Re: Palindrome Help

Posted 25 August 2019 - 01:36 PM

Oh I see now, i'm a beginner and I completely forgot that a nested loop runs every iteration before returning to the outer loop. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#5 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 139
  • Joined: 26-September 16

Re: Palindrome Help

Posted 30 August 2019 - 04:56 AM

Here are my two cents:

It is good practice to pass objects like strings by const reference, to avoid copying and prevent modification. It may not matter in a simple example like this, but it's something to consider. So, instead of checkIfPalindrome(string input), you would write checkIfPalindrome(const string& input)

It doesn't really matter here, but in larger projects, you may want to use std::string (instead of declaring using namespace std;) to prevent potential name clashes.

As for the algorithm, as sepp2k has pointed out, rather than saying that bool isPalindrome = false;, it's better to start with the assumption that the input string is a palindrome, ie. bool isPalindrome = true;, because it's easier to prove that it isn't than that it is.

Also, you could easily use a single for-loop, eg. for (size_t i = 0, j = input.size() - 1; i < input.size() / 2; ++i, --j), and break out of it as soon as you found an inequality.

This post has been edited by GazinAtCode: 30 August 2019 - 05:05 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1