-1

I wrote this function isPalindromic for my compsci class where the professor wants us to better understand how library functions work. So he asked us to write a function isPalindromic and mine isn't working as well. Because there are so many parts, I will paste the whole function, so bear with me. Sorry!

The function always returns false for some reason. The word passed is "HELLO ". My first loop checks for the size of the word without spaces or null characters so I can use it as a parameter in my second loop. This returns false, which is correct, but when I pass "HELLEH " or "HELLEH", they both return false. I've rewritten this at least 5 times, and I can't figure out why it's returning false.

char* isPalindromic(char inputCheck[]){
    int actWord;
    int sizeCheck = myStrLen(inputCheck);
    char tempWord[actWord];
    for(int check = 0; check <  sizeCheck; check++){
        if(inputCheck[check] = ' ' || inputCheck[check] == '\0')
            actWord = check;
    }

    for(int replace = 0; replace < actWord; replace++){
        tempWord[replace] = inputCheck[actWord - replace];
    } 

    tempWord == inputCheck ? inputCheck = "True" : inputCheck = "False";
    return inputCheck;
}
cmaher
  • 5,100
  • 1
  • 22
  • 34
Vincent
  • 99
  • 5
  • 5
    What happened to using `std::string`? – WhiZTiM Feb 15 '18 at 23:12
  • 1
    actWord is not initialized – Mitch Feb 15 '18 at 23:13
  • tempWord is a single character that you are comparing against a string – Mitch Feb 15 '18 at 23:14
  • @Mitchel0022 even initializing int actWord = 0, it returns false. WHIZTIM, we can't use any functions. Everything must be our code. – Vincent Feb 15 '18 at 23:14
  • **Try something simpler first.** Try writing a function that tests whether a given string is "HELLO". – Beta Feb 15 '18 at 23:15
  • @Mitchel0022 how is it? It is looking and changing the index one by one? – Vincent Feb 15 '18 at 23:16
  • `tempWord == inputCheck` does not compare strings. It compare only pointers. Lookup `strcmp`. – R Sahu Feb 15 '18 at 23:16
  • because you defined temp word as a char – Mitch Feb 15 '18 at 23:16
  • @Vincent: Then you might as well just use C. Pretty sure `char tempWord[actWord];` isn't valid in C++ anyway (C++ doesn't support VLAs), but it's OK in C (or would be if you'd given `actWord` a value). – cHao Feb 15 '18 at 23:17
  • 3
    A warning: The question states, *" to better understand how library functions work."* but the given code uses no library functions. – user4581301 Feb 15 '18 at 23:19
  • "we can't use any functions. Everything must be our code. " - so in order to test this presumably you have re-implemented the iostream library? –  Feb 15 '18 at 23:23
  • _"bare with me"_ Prime material for a sexual harassment lawsuit er I meant public outing on Instagram. Think you meant "bear"... – Lightness Races in Orbit Feb 15 '18 at 23:31
  • @user4581301: OP is trying to better understand how library functions by reimplementing one, obviously – Lightness Races in Orbit Feb 15 '18 at 23:32
  • @LightnessRacesinOrbit that's a more reasonable read of the question given the content. – user4581301 Feb 15 '18 at 23:33
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) If you are not using a step debugger you should, because it would show you exactly why you are not doing what you think you are doing. –  Feb 15 '18 at 23:35
  • 1
    Aside: https://english.stackexchange.com/questions/1269/is-it-bear-or-bare-with-me – aschepler Feb 15 '18 at 23:36

1 Answers1

4
char tempWord[actWord];

actWord at this point is uninitialised. Your entire program therefore has undefined behaviour.


tempWord == inputCheck ? inputCheck = "True" : inputCheck = "False"; 

This is also a problem; you cannot compare two character arrays with == like this; you're just comparing their locations in memory. You'll have to use reimplement strcmp for that (although, actually, a much simpler version of your algorithm will not require such logic).


You don't need any of this extra buffer space. All you need to do is iterate from front and back simultaneously, comparing characters.

const char* isPalindromic(const char inputCheck[])
{
    const int size = myStrLen(inputCheck);
    for (size_t i1 = 0, i2 = size-1; i1 < i2; i1++, i2--)
       if (inputCheck[i1] != inputCheck[i2])
          return "False";
    return "True";
}

(live demo)

Also I would strongly consider returning a bool, not "True" or "False".

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    IMO, "1" or "Zero" would be better - 1 is palindromic, "Zero" isn't ;) (Seen your flag, we'll keep an eye out.) – BoltClock Feb 16 '18 at 04:27