Palindrome checking function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
14
down vote

favorite
2












I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.



My code passed the automated tests on the website, but I understand those aren't perfect.



class Palindrome

public static function isPalindrome($word)

$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);

if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


$secondHalfReversed = strrev($secondHalf);

if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;




echo Palindrome::isPalindrome('Deleveled');






share|improve this question

















  • 5




    Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
    – Quelklef
    May 22 at 2:49






  • 1




    @Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
    – Matadeleo
    May 22 at 9:07
















up vote
14
down vote

favorite
2












I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.



My code passed the automated tests on the website, but I understand those aren't perfect.



class Palindrome

public static function isPalindrome($word)

$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);

if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


$secondHalfReversed = strrev($secondHalf);

if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;




echo Palindrome::isPalindrome('Deleveled');






share|improve this question

















  • 5




    Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
    – Quelklef
    May 22 at 2:49






  • 1




    @Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
    – Matadeleo
    May 22 at 9:07












up vote
14
down vote

favorite
2









up vote
14
down vote

favorite
2






2





I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.



My code passed the automated tests on the website, but I understand those aren't perfect.



class Palindrome

public static function isPalindrome($word)

$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);

if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


$secondHalfReversed = strrev($secondHalf);

if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;




echo Palindrome::isPalindrome('Deleveled');






share|improve this question













I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.



My code passed the automated tests on the website, but I understand those aren't perfect.



class Palindrome

public static function isPalindrome($word)

$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);

if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


$secondHalfReversed = strrev($secondHalf);

if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;




echo Palindrome::isPalindrome('Deleveled');








share|improve this question












share|improve this question




share|improve this question








edited May 21 at 17:27









Sam Onela

5,75961543




5,75961543









asked May 21 at 17:16









Matadeleo

18317




18317







  • 5




    Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
    – Quelklef
    May 22 at 2:49






  • 1




    @Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
    – Matadeleo
    May 22 at 9:07












  • 5




    Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
    – Quelklef
    May 22 at 2:49






  • 1




    @Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
    – Matadeleo
    May 22 at 9:07







5




5




Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
– Quelklef
May 22 at 2:49




Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
– Quelklef
May 22 at 2:49




1




1




@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
– Matadeleo
May 22 at 9:07




@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the isPalindrome function
– Matadeleo
May 22 at 9:07










5 Answers
5






active

oldest

votes

















up vote
33
down vote



accepted










Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".



Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.



public static function isPalindrome($word)

return $word == strrev($word);




Update



Based on suggestions in the comments, this would be nicer:



function isPalindrome($word) 
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);



That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)






share|improve this answer























  • Smart :) I like it!
    – Matadeleo
    May 21 at 22:12










  • And what is the reason of the divorce?
    – Billal BEGUERADJ
    May 22 at 10:53






  • 3




    You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
    – Tonny
    May 22 at 11:26






  • 1




    this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
    – Doug
    May 22 at 12:58






  • 2




    @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
    – Scott Sauyet
    May 22 at 14:13

















up vote
7
down vote













To start with id remove the code that you written twice by re-arranging the if statement into this



if ($wordLength % 2 == 0) 
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);


$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


Then for the return value I would use;



return $firstHalf == $secondHalfReversed;


This is about all i would change






share|improve this answer





















  • Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
    – Matadeleo
    May 21 at 18:35

















up vote
6
down vote













This is a very good program. There is only one thing I'd change:




if ($firstHalf == $secondHalfReversed) 
return TRUE;
else
return FALSE;




You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:



return $firstHalf == $secondHalfReversed





share|improve this answer




























    up vote
    2
    down vote













    I'd write this using a traditional for loop as follows:



    class Palindrome

    public static function isPalindrome($word)

    $wordLength = strlen($word)-1;

    for ($i = 0; $i < $wordLength/2; $i++)
    if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
    return FALSE;



    return TRUE;



    echo Palindrome::isPalindrome('Deleveled');





    share|improve this answer

















    • 1




      Pedantically, this approach would be less computing power than the other approaches.
      – Kenneth K.
      May 22 at 13:17

















    up vote
    0
    down vote













    echo Palindrome::isPalindrome('Deleveled');


    You need to test corner cases and try to break your code. For example, you might try with:



    • a string of odd size

    • a string of even size

    • the empty string

    • a string with one character

    How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?






    share|improve this answer





















    • The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
      – Matadeleo
      May 23 at 9:14










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194879%2fpalindrome-checking-function%23new-answer', 'question_page');

    );

    Post as a guest






























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    33
    down vote



    accepted










    Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".



    Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.



    public static function isPalindrome($word)

    return $word == strrev($word);




    Update



    Based on suggestions in the comments, this would be nicer:



    function isPalindrome($word) 
    $word = preg_replace('/[^a-zA-Z]/', '', $word);
    $word = strtolower($word);
    return $word == strrev($word);



    That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)






    share|improve this answer























    • Smart :) I like it!
      – Matadeleo
      May 21 at 22:12










    • And what is the reason of the divorce?
      – Billal BEGUERADJ
      May 22 at 10:53






    • 3




      You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
      – Tonny
      May 22 at 11:26






    • 1




      this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
      – Doug
      May 22 at 12:58






    • 2




      @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
      – Scott Sauyet
      May 22 at 14:13














    up vote
    33
    down vote



    accepted










    Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".



    Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.



    public static function isPalindrome($word)

    return $word == strrev($word);




    Update



    Based on suggestions in the comments, this would be nicer:



    function isPalindrome($word) 
    $word = preg_replace('/[^a-zA-Z]/', '', $word);
    $word = strtolower($word);
    return $word == strrev($word);



    That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)






    share|improve this answer























    • Smart :) I like it!
      – Matadeleo
      May 21 at 22:12










    • And what is the reason of the divorce?
      – Billal BEGUERADJ
      May 22 at 10:53






    • 3




      You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
      – Tonny
      May 22 at 11:26






    • 1




      this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
      – Doug
      May 22 at 12:58






    • 2




      @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
      – Scott Sauyet
      May 22 at 14:13












    up vote
    33
    down vote



    accepted







    up vote
    33
    down vote



    accepted






    Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".



    Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.



    public static function isPalindrome($word)

    return $word == strrev($word);




    Update



    Based on suggestions in the comments, this would be nicer:



    function isPalindrome($word) 
    $word = preg_replace('/[^a-zA-Z]/', '', $word);
    $word = strtolower($word);
    return $word == strrev($word);



    That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)






    share|improve this answer















    Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".



    Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.



    public static function isPalindrome($word)

    return $word == strrev($word);




    Update



    Based on suggestions in the comments, this would be nicer:



    function isPalindrome($word) 
    $word = preg_replace('/[^a-zA-Z]/', '', $word);
    $word = strtolower($word);
    return $word == strrev($word);



    That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited May 22 at 14:10


























    answered May 21 at 20:46









    Scott Sauyet

    59438




    59438











    • Smart :) I like it!
      – Matadeleo
      May 21 at 22:12










    • And what is the reason of the divorce?
      – Billal BEGUERADJ
      May 22 at 10:53






    • 3




      You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
      – Tonny
      May 22 at 11:26






    • 1




      this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
      – Doug
      May 22 at 12:58






    • 2




      @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
      – Scott Sauyet
      May 22 at 14:13
















    • Smart :) I like it!
      – Matadeleo
      May 21 at 22:12










    • And what is the reason of the divorce?
      – Billal BEGUERADJ
      May 22 at 10:53






    • 3




      You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
      – Tonny
      May 22 at 11:26






    • 1




      this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
      – Doug
      May 22 at 12:58






    • 2




      @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
      – Scott Sauyet
      May 22 at 14:13















    Smart :) I like it!
    – Matadeleo
    May 21 at 22:12




    Smart :) I like it!
    – Matadeleo
    May 21 at 22:12












    And what is the reason of the divorce?
    – Billal BEGUERADJ
    May 22 at 10:53




    And what is the reason of the divorce?
    – Billal BEGUERADJ
    May 22 at 10:53




    3




    3




    You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
    – Tonny
    May 22 at 11:26




    You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
    – Tonny
    May 22 at 11:26




    1




    1




    this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
    – Doug
    May 22 at 12:58




    this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
    – Doug
    May 22 at 12:58




    2




    2




    @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
    – Scott Sauyet
    May 22 at 14:13




    @BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
    – Scott Sauyet
    May 22 at 14:13












    up vote
    7
    down vote













    To start with id remove the code that you written twice by re-arranging the if statement into this



    if ($wordLength % 2 == 0) 
    $firstHalf = substr($word, 0, $wordSplitPoint);
    else
    $firstHalf = substr($word, 0, $wordSplitPoint-1);


    $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


    Then for the return value I would use;



    return $firstHalf == $secondHalfReversed;


    This is about all i would change






    share|improve this answer





















    • Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
      – Matadeleo
      May 21 at 18:35














    up vote
    7
    down vote













    To start with id remove the code that you written twice by re-arranging the if statement into this



    if ($wordLength % 2 == 0) 
    $firstHalf = substr($word, 0, $wordSplitPoint);
    else
    $firstHalf = substr($word, 0, $wordSplitPoint-1);


    $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


    Then for the return value I would use;



    return $firstHalf == $secondHalfReversed;


    This is about all i would change






    share|improve this answer





















    • Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
      – Matadeleo
      May 21 at 18:35












    up vote
    7
    down vote










    up vote
    7
    down vote









    To start with id remove the code that you written twice by re-arranging the if statement into this



    if ($wordLength % 2 == 0) 
    $firstHalf = substr($word, 0, $wordSplitPoint);
    else
    $firstHalf = substr($word, 0, $wordSplitPoint-1);


    $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


    Then for the return value I would use;



    return $firstHalf == $secondHalfReversed;


    This is about all i would change






    share|improve this answer













    To start with id remove the code that you written twice by re-arranging the if statement into this



    if ($wordLength % 2 == 0) 
    $firstHalf = substr($word, 0, $wordSplitPoint);
    else
    $firstHalf = substr($word, 0, $wordSplitPoint-1);


    $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);


    Then for the return value I would use;



    return $firstHalf == $secondHalfReversed;


    This is about all i would change







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered May 21 at 18:11









    Dan

    373211




    373211











    • Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
      – Matadeleo
      May 21 at 18:35
















    • Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
      – Matadeleo
      May 21 at 18:35















    Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
    – Matadeleo
    May 21 at 18:35




    Thanks, no reason why the $secondHalf should be repeated, and that return is much cleaner
    – Matadeleo
    May 21 at 18:35










    up vote
    6
    down vote













    This is a very good program. There is only one thing I'd change:




    if ($firstHalf == $secondHalfReversed) 
    return TRUE;
    else
    return FALSE;




    You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:



    return $firstHalf == $secondHalfReversed





    share|improve this answer

























      up vote
      6
      down vote













      This is a very good program. There is only one thing I'd change:




      if ($firstHalf == $secondHalfReversed) 
      return TRUE;
      else
      return FALSE;




      You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:



      return $firstHalf == $secondHalfReversed





      share|improve this answer























        up vote
        6
        down vote










        up vote
        6
        down vote









        This is a very good program. There is only one thing I'd change:




        if ($firstHalf == $secondHalfReversed) 
        return TRUE;
        else
        return FALSE;




        You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:



        return $firstHalf == $secondHalfReversed





        share|improve this answer













        This is a very good program. There is only one thing I'd change:




        if ($firstHalf == $secondHalfReversed) 
        return TRUE;
        else
        return FALSE;




        You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:



        return $firstHalf == $secondHalfReversed






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 21 at 18:09









        Hosch250

        16.9k561153




        16.9k561153




















            up vote
            2
            down vote













            I'd write this using a traditional for loop as follows:



            class Palindrome

            public static function isPalindrome($word)

            $wordLength = strlen($word)-1;

            for ($i = 0; $i < $wordLength/2; $i++)
            if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
            return FALSE;



            return TRUE;



            echo Palindrome::isPalindrome('Deleveled');





            share|improve this answer

















            • 1




              Pedantically, this approach would be less computing power than the other approaches.
              – Kenneth K.
              May 22 at 13:17














            up vote
            2
            down vote













            I'd write this using a traditional for loop as follows:



            class Palindrome

            public static function isPalindrome($word)

            $wordLength = strlen($word)-1;

            for ($i = 0; $i < $wordLength/2; $i++)
            if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
            return FALSE;



            return TRUE;



            echo Palindrome::isPalindrome('Deleveled');





            share|improve this answer

















            • 1




              Pedantically, this approach would be less computing power than the other approaches.
              – Kenneth K.
              May 22 at 13:17












            up vote
            2
            down vote










            up vote
            2
            down vote









            I'd write this using a traditional for loop as follows:



            class Palindrome

            public static function isPalindrome($word)

            $wordLength = strlen($word)-1;

            for ($i = 0; $i < $wordLength/2; $i++)
            if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
            return FALSE;



            return TRUE;



            echo Palindrome::isPalindrome('Deleveled');





            share|improve this answer













            I'd write this using a traditional for loop as follows:



            class Palindrome

            public static function isPalindrome($word)

            $wordLength = strlen($word)-1;

            for ($i = 0; $i < $wordLength/2; $i++)
            if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
            return FALSE;



            return TRUE;



            echo Palindrome::isPalindrome('Deleveled');






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 21 at 22:30









            aidanharris

            734




            734







            • 1




              Pedantically, this approach would be less computing power than the other approaches.
              – Kenneth K.
              May 22 at 13:17












            • 1




              Pedantically, this approach would be less computing power than the other approaches.
              – Kenneth K.
              May 22 at 13:17







            1




            1




            Pedantically, this approach would be less computing power than the other approaches.
            – Kenneth K.
            May 22 at 13:17




            Pedantically, this approach would be less computing power than the other approaches.
            – Kenneth K.
            May 22 at 13:17










            up vote
            0
            down vote













            echo Palindrome::isPalindrome('Deleveled');


            You need to test corner cases and try to break your code. For example, you might try with:



            • a string of odd size

            • a string of even size

            • the empty string

            • a string with one character

            How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?






            share|improve this answer





















            • The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
              – Matadeleo
              May 23 at 9:14














            up vote
            0
            down vote













            echo Palindrome::isPalindrome('Deleveled');


            You need to test corner cases and try to break your code. For example, you might try with:



            • a string of odd size

            • a string of even size

            • the empty string

            • a string with one character

            How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?






            share|improve this answer





















            • The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
              – Matadeleo
              May 23 at 9:14












            up vote
            0
            down vote










            up vote
            0
            down vote









            echo Palindrome::isPalindrome('Deleveled');


            You need to test corner cases and try to break your code. For example, you might try with:



            • a string of odd size

            • a string of even size

            • the empty string

            • a string with one character

            How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?






            share|improve this answer













            echo Palindrome::isPalindrome('Deleveled');


            You need to test corner cases and try to break your code. For example, you might try with:



            • a string of odd size

            • a string of even size

            • the empty string

            • a string with one character

            How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 23 at 8:02









            coredump

            678311




            678311











            • The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
              – Matadeleo
              May 23 at 9:14
















            • The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
              – Matadeleo
              May 23 at 9:14















            The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
            – Matadeleo
            May 23 at 9:14




            The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
            – Matadeleo
            May 23 at 9:14












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194879%2fpalindrome-checking-function%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation