-1

I can't for the life of me get my code to work. It identifies palindromes correctly, but for some reason, some non-palindromes words get marked as palindromes. Not all, just sum. And biggest headache of all, I can't figure out the correlation between of the non-palindromes that pass.

Any other feedback is appreciated.

#include <iostream> 
#include <ctype.h> 
#include <string.h> 
#include <limits> 

using namespace std;

int main() {

const int a(15);
char Line[a + 1];
int i;


do {
    cout << "Enter a possible palindrome" << endl;
    cin.getline(Line, a + 1);
    if (cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    else;

    for (int i = 0; i < strlen(Line); i++) {
        Line[i] = (char)tolower(Line[i]);
    }

    int c = strlen(Line);
    for (int i = 0; i < c / 2; i++) {


        while (!(((int)Line[c - 1 - i] >= 'a' && (int)Line[c - 1 - i] <= 'z') || ((int)Line[c - 1 - i] >= 'A' && (int)Line[c - 1 - i] <= 'Z'))) {
            c--;
        }

        if ((Line[i] == Line[c - 1 - i]))
        {
            cout << "is a Palindrome" << endl;

        }
        else
            cout << Line << " is not a palindrome." << endl;
        break;

    }




} while (strcmp(Line, "END") != 0);

return 0;
  • 3
    Can you give us an example of a word that is incorrectly identified as a palindrome? And have you tried to step through the code line by line in a debugger? – Some programmer dude Feb 25 '17 at 05:24
  • `while (!(((int)Line[c - 1 - i] >= 'a' && (int)Line[c - 1 - i] <= 'z') || ((int)Line[c - 1 - i] >= 'A' && (int)Line[c - 1 - i] <= 'Z'))) ` -- There is an `isalpha()` function in C++ that replaces all of that. In addition, it works for ASCII and non-ASCII systems. – PaulMcKenzie Feb 25 '17 at 05:26
  • 1
    If you step through your code in a debugger you will immediately see the problem. Please do learn how to debug, it will save you from a lot of trouble and is a lot faster than waiting for someone to tell you. Hint: it will say "antikythera" is a palindrome but not "bananas" – Sami Kuhmonen Feb 25 '17 at 05:29
  • I think I figured out the correlation- they all start and end with the same letter. I'm a little embarrassed I hadn't caught that. – velaaelias Feb 25 '17 at 05:30
  • It seems that your program doesn't check for numbers as well. That should be easy to incorporate. – lakshayg Feb 25 '17 at 05:30
  • As a newbie, have fun with this: char *rev = strrev (strdup (Line)); if (strcmpi (Line, rev) == 0) cout<<" is a pallindrome"; free (rev); – Mike Feb 25 '17 at 05:46

2 Answers2

0

The string is a palindrome if the condition Line[i] == Line[c-1-i] holds for all i < c/2. You print out that its a palindrome provided two of the characters match.

Eg: Your program would say:

"abdca" //is a palindrome since the first and last character match.
Janarth K
  • 152
  • 6
0

I think your code is intricacy a bit. Let's assume that the input is always readable, so you just need to cin >> Line;. Let n is length of string. Now we use a loop from 0 to n / 2 to check the symmetry of string. If Line[i] != Line[n - i - 1] that means Line is not symmetry (palindrome) then we just need to print the result and return 0. If the program pass the loop that mean Line is pallindrome. This problem is quite easy. For me, the way you think of it is complex a bit.