-1
#include <iostream>
#include <cstring>
#include <string>
#include <ctype.h>
using namespace std;

//declare methods
void transform(char *, char *);
bool testPalindrome(char *);


int main()
{
    //declare c-strings and a boolean variable
    char str[80];
    char myStr[100];
    char defaultValue[] = "0";
    bool done = true;

    //continously ask a user to enter a string until the user enters 0
    while (done)
    {
        cout << "Enter a string (0 to exit the program): ";
        cin.getline(str, 80);
        if (strcmp(str,defaultValue) == 0)
        {
            done = false;
        }
        else
        {
            //convert all lowercase letters of user input to uppercase
            transform(str, myStr);

            //test if user input is the same when read backward and forward
            if (testPalindrome(myStr))
            {
                cout << "It is a palindrome." << endl;
            }
            else
            {
                cout << "It is not a palindrome." << endl;
            }
        }
    }
    system("pause");
    return 0;
}
/*
This method converts all lowercase letters into uppercase letters
as well as removes characters that are not numbers or letters.
The new string will be stored in the C-string testStr
*/
void transform(char * raw, char * testStr)
{
    int length = strlen(raw);
    int j = 0;

    //convert all lowercase letters to uppercase letters if current letter      is lowercase
for(int i = 0; i < length; i++)
{
    if (islower(raw[i])) 
    {
        raw[i] = toupper(raw[i]);
    }

}
//copy user input, remove all characters that are letters or numbers
for (int k = 0; k < length; k++)
{
    if ((isalpha(raw[k]) || isdigit(raw[k])))
    {
        *(testStr + k) = *(raw + k);
    }
    else if (isspace(raw[k]))
    {
        const char * current = &raw[k];
        remove(current);
    }
    j++;
}
*(testStr + j) = '\0';
}
/*
This method determines if the user input can be read the same backward or       forward.
will take a parameter of a pointer variable, which refers to
memory address of the c-string
*/
bool testPalindrome(char * str)
    {
        int test = 1;
        bool flag = true;
        int length = strlen(str) - 1;
        int n = length;
       for (int i = 0; i <= length && n >= 0; i++, n--) 
       {
             if (toupper(str[i]) == toupper(str[n]))
             {
                  flag = true;
             }
             else
             {
                 flag = false;
             }
       }
    return flag;
}

In this program I am trying to display whether user input is a palindrome. I am given 5 strings to test:

Radar

Too hot to hoot

Madam!I'm Adam

A man, A plan, A canal-Panama

Doc, note, I dissent! A fast never prevents a fatness; I diet on cod

For some reason, my program passes for 4 out of these 5 cases. In addition, I tested the non-palindromes and it seems to work just fine. The image below illustrates the results of the 5 strings being user input:

https://i.stack.imgur.com/cYmHe.png

As you can see in the image, the string "A man, A plan, A canal-Panama" gives the opposite result, while the other strings provided the anticipated result. Where in my code is causing this? Advice and/or constructive criticism would be really helpful.

Community
  • 1
  • 1
Minhkhoa Vu
  • 19
  • 2
  • 6

1 Answers1

0

For you example code, The main issue is in your second for loop in transform method. Here's the fixed version.

// copy str, remove all characters that are NOT letters or numbers
for (int k = 0; k < length; k++)
{
    if ((isalpha(raw[k]) || isdigit(raw[k])))
    {
        *(testStr + j++) = *(raw + k);
    }
}

Btw, there're several other issues in your code, I suggest you to take a look at the good solutions in leetcode here.

Levi
  • 185
  • 2
  • 11