0

The purpose of this code is to determine whether s1 and s2 are palindromes using bool isPalindrome and int length = strlen(cstr). Why is this not working? What are the issues here?

#include<iostream>
using namespace std;

bool isPalindrome(char*cstr);

bool isPalindrome(char*cstr)
{

    int length = strlen(cstr);

    int temp, arr[length];
    
       int j = length - 1;
       for (int i = 0; i < j; i++) {
           temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
           j--;
       }

    if ((arr[length] = *cstr))
        return true;
    else
        return false;

}


int main ()
{

    char s1[50] = "neveroddoreven";
    char s2[50] = "notapalindrome";


    cout<<isPalindrome(s1)<<endl;
    cout<<isPalindrome(s2)<<endl;

    return 0;


}
  • `arr[length];` -- This is not legal C++. Arrays in C++ must have their size denoted by a compile time constant, not a runtime variable. Use `std::string` – PaulMcKenzie Mar 09 '21 at 05:50
  • Thanks. I will use that, but it does not seem to resolve the issue of the bool function not working properly. – W. Atkinson Mar 09 '21 at 05:52
  • `if ((arr[length] = *cstr))` -- What operator is used to compare two values? It isn't `=`. Second, `arr` isn't set to anything -- what is that `for` loop supposed to do with it? – PaulMcKenzie Mar 09 '21 at 05:56
  • What makes you think it is not working? What are the issues here? – JaMiT Mar 09 '21 at 05:56
  • It returns "1""1" meaning that it does not correctly determine that one is a palindrome and the other is not. – W. Atkinson Mar 09 '21 at 06:00
  • With the correct == it is now "0" and "0" rather than "1" and "1." – W. Atkinson Mar 09 '21 at 06:02
  • I am trying to compare the reversed array to the original array. What would be a better way to do this? – W. Atkinson Mar 09 '21 at 06:02
  • You never addressed what `arr` is supposed to be doing. It is not assigned anything. You declared it, and you start using this array without any elements of it assigned to a value. Basically you're swapping junk values in that loop. – PaulMcKenzie Mar 09 '21 at 06:12
  • I see, but what should I assign to it? – W. Atkinson Mar 09 '21 at 06:13
  • @W.Atkinson -- There are many answers as to how to do palindrome testing, many with single-line solutions using `std::string` and comparing the reverse of the string. Have you done a search? – PaulMcKenzie Mar 09 '21 at 06:19
  • Yes. My current goal is to modify this program to work without making drastic changes. – W. Atkinson Mar 09 '21 at 06:21
  • Obviously you should assign the contents of the original array to `arr` before you start swapping it, no? Have you worked this out on paper, as that should have been the obvious missing piece. Remember that program do *exactly* what you tell it to do. You declared an array, didn't set any values in it, and expected it to "work". Programming doesn't work this way, where the program guesses what your intent is. – PaulMcKenzie Mar 09 '21 at 06:40

0 Answers0