Palindrome algorithm

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
-1
down vote

favorite












This code could be better?



This is the algorithm:



  1. Compare the 1st character to the last character

  2. Compare the 2nd character to the second last character and so on

  3. Stop when the middle of the string is reached

  4. Just words

namespace Palindrome

class Class1

static void Main(string args)

while (true)

Console.WriteLine("Digit a word:");
string word = Console.ReadLine();

if (word == "exit")
break;

VerifyPalindrome(word);



private static void VerifyPalindrome(string word)

bool compare = true;
int i = 0;
while (i < word.Length- 1)

if (compare)

for (int j = word.Length - 1; j >= 0; j--)

if (word[i] == word[j])
compare = true;
else

compare = false;
break;

i++;


else
break;


if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");









share|improve this question

















  • 2




    Your code claims that foo and ba are palindromes.
    – Marvin
    Feb 24 at 10:41











  • The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
    – Pankwood
    Feb 24 at 16:10











  • I've fixed the code.
    – Pankwood
    Mar 1 at 1:25
















up vote
-1
down vote

favorite












This code could be better?



This is the algorithm:



  1. Compare the 1st character to the last character

  2. Compare the 2nd character to the second last character and so on

  3. Stop when the middle of the string is reached

  4. Just words

namespace Palindrome

class Class1

static void Main(string args)

while (true)

Console.WriteLine("Digit a word:");
string word = Console.ReadLine();

if (word == "exit")
break;

VerifyPalindrome(word);



private static void VerifyPalindrome(string word)

bool compare = true;
int i = 0;
while (i < word.Length- 1)

if (compare)

for (int j = word.Length - 1; j >= 0; j--)

if (word[i] == word[j])
compare = true;
else

compare = false;
break;

i++;


else
break;


if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");









share|improve this question

















  • 2




    Your code claims that foo and ba are palindromes.
    – Marvin
    Feb 24 at 10:41











  • The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
    – Pankwood
    Feb 24 at 16:10











  • I've fixed the code.
    – Pankwood
    Mar 1 at 1:25












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











This code could be better?



This is the algorithm:



  1. Compare the 1st character to the last character

  2. Compare the 2nd character to the second last character and so on

  3. Stop when the middle of the string is reached

  4. Just words

namespace Palindrome

class Class1

static void Main(string args)

while (true)

Console.WriteLine("Digit a word:");
string word = Console.ReadLine();

if (word == "exit")
break;

VerifyPalindrome(word);



private static void VerifyPalindrome(string word)

bool compare = true;
int i = 0;
while (i < word.Length- 1)

if (compare)

for (int j = word.Length - 1; j >= 0; j--)

if (word[i] == word[j])
compare = true;
else

compare = false;
break;

i++;


else
break;


if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");









share|improve this question













This code could be better?



This is the algorithm:



  1. Compare the 1st character to the last character

  2. Compare the 2nd character to the second last character and so on

  3. Stop when the middle of the string is reached

  4. Just words

namespace Palindrome

class Class1

static void Main(string args)

while (true)

Console.WriteLine("Digit a word:");
string word = Console.ReadLine();

if (word == "exit")
break;

VerifyPalindrome(word);



private static void VerifyPalindrome(string word)

bool compare = true;
int i = 0;
while (i < word.Length- 1)

if (compare)

for (int j = word.Length - 1; j >= 0; j--)

if (word[i] == word[j])
compare = true;
else

compare = false;
break;

i++;


else
break;


if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");











share|improve this question












share|improve this question




share|improve this question








edited Mar 1 at 1:24
























asked Feb 23 at 22:30









Pankwood

1014




1014







  • 2




    Your code claims that foo and ba are palindromes.
    – Marvin
    Feb 24 at 10:41











  • The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
    – Pankwood
    Feb 24 at 16:10











  • I've fixed the code.
    – Pankwood
    Mar 1 at 1:25












  • 2




    Your code claims that foo and ba are palindromes.
    – Marvin
    Feb 24 at 10:41











  • The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
    – Pankwood
    Feb 24 at 16:10











  • I've fixed the code.
    – Pankwood
    Mar 1 at 1:25







2




2




Your code claims that foo and ba are palindromes.
– Marvin
Feb 24 at 10:41





Your code claims that foo and ba are palindromes.
– Marvin
Feb 24 at 10:41













The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
– Pankwood
Feb 24 at 16:10





The problem is on while clausule: while (i < (word.Length / 2) - 1) Thanks
– Pankwood
Feb 24 at 16:10













I've fixed the code.
– Pankwood
Mar 1 at 1:25




I've fixed the code.
– Pankwood
Mar 1 at 1:25










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










As already stated you don't need to set it to true multiple times.

There is cleaner syntax for this.



static bool IsPalidrone(string word)

for (int i = 0, j = word.Length - 1; i < j; i++, j--)

if (word[i] != word[j])

return false;


return true;






share|improve this answer





















  • Clean and smart! Thanks!
    – Pankwood
    Feb 24 at 18:24










  • @DaniloDebiaziVicente You could give it the check if it is the answer.
    – paparazzo
    Feb 24 at 18:31










  • I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
    – Pankwood
    Feb 26 at 1:04










  • @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
    – paparazzo
    Feb 26 at 6:33










  • @Pararazzi I got it!
    – Pankwood
    Feb 27 at 22:48

















up vote
2
down vote













One thing I would like to change is the responsibilities of VerifyPalindrome(string word). Remember, your function should only check whether word is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:



private static bool VerifyPalindrome(string word)

bool compare = true;

// ... computing code

return compare;



And in Main:



if(VerifyPalindrome(word))

Console.WriteLine("This is a Palindrome word");

else

Console.WriteLine("This is NOT a Palindrome word");



The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare for whether a string is a palindrome or not and using breaks, which increases the complexity of your code, you can reduce the function to the following, with multiple returns:



private static bool VerifyPalindrome(string word)

int i = 0;
while (i < (word.Length / 2) - 1)

for (int j = word.Length - 1; j >= 0; j--)

if (word[i] != word[j])
return false;
i++;


return true;



No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.






share|improve this answer























    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%2f188234%2fpalindrome-algorithm%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    As already stated you don't need to set it to true multiple times.

    There is cleaner syntax for this.



    static bool IsPalidrone(string word)

    for (int i = 0, j = word.Length - 1; i < j; i++, j--)

    if (word[i] != word[j])

    return false;


    return true;






    share|improve this answer





















    • Clean and smart! Thanks!
      – Pankwood
      Feb 24 at 18:24










    • @DaniloDebiaziVicente You could give it the check if it is the answer.
      – paparazzo
      Feb 24 at 18:31










    • I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
      – Pankwood
      Feb 26 at 1:04










    • @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
      – paparazzo
      Feb 26 at 6:33










    • @Pararazzi I got it!
      – Pankwood
      Feb 27 at 22:48














    up vote
    3
    down vote



    accepted










    As already stated you don't need to set it to true multiple times.

    There is cleaner syntax for this.



    static bool IsPalidrone(string word)

    for (int i = 0, j = word.Length - 1; i < j; i++, j--)

    if (word[i] != word[j])

    return false;


    return true;






    share|improve this answer





















    • Clean and smart! Thanks!
      – Pankwood
      Feb 24 at 18:24










    • @DaniloDebiaziVicente You could give it the check if it is the answer.
      – paparazzo
      Feb 24 at 18:31










    • I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
      – Pankwood
      Feb 26 at 1:04










    • @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
      – paparazzo
      Feb 26 at 6:33










    • @Pararazzi I got it!
      – Pankwood
      Feb 27 at 22:48












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    As already stated you don't need to set it to true multiple times.

    There is cleaner syntax for this.



    static bool IsPalidrone(string word)

    for (int i = 0, j = word.Length - 1; i < j; i++, j--)

    if (word[i] != word[j])

    return false;


    return true;






    share|improve this answer













    As already stated you don't need to set it to true multiple times.

    There is cleaner syntax for this.



    static bool IsPalidrone(string word)

    for (int i = 0, j = word.Length - 1; i < j; i++, j--)

    if (word[i] != word[j])

    return false;


    return true;







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Feb 23 at 23:45









    paparazzo

    4,8131730




    4,8131730











    • Clean and smart! Thanks!
      – Pankwood
      Feb 24 at 18:24










    • @DaniloDebiaziVicente You could give it the check if it is the answer.
      – paparazzo
      Feb 24 at 18:31










    • I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
      – Pankwood
      Feb 26 at 1:04










    • @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
      – paparazzo
      Feb 26 at 6:33










    • @Pararazzi I got it!
      – Pankwood
      Feb 27 at 22:48
















    • Clean and smart! Thanks!
      – Pankwood
      Feb 24 at 18:24










    • @DaniloDebiaziVicente You could give it the check if it is the answer.
      – paparazzo
      Feb 24 at 18:31










    • I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
      – Pankwood
      Feb 26 at 1:04










    • @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
      – paparazzo
      Feb 26 at 6:33










    • @Pararazzi I got it!
      – Pankwood
      Feb 27 at 22:48















    Clean and smart! Thanks!
    – Pankwood
    Feb 24 at 18:24




    Clean and smart! Thanks!
    – Pankwood
    Feb 24 at 18:24












    @DaniloDebiaziVicente You could give it the check if it is the answer.
    – paparazzo
    Feb 24 at 18:31




    @DaniloDebiaziVicente You could give it the check if it is the answer.
    – paparazzo
    Feb 24 at 18:31












    I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
    – Pankwood
    Feb 26 at 1:04




    I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
    – Pankwood
    Feb 26 at 1:04












    @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
    – paparazzo
    Feb 26 at 6:33




    @DaniloDebiaziVicente Up vote and check are not the same. But no problem.
    – paparazzo
    Feb 26 at 6:33












    @Pararazzi I got it!
    – Pankwood
    Feb 27 at 22:48




    @Pararazzi I got it!
    – Pankwood
    Feb 27 at 22:48












    up vote
    2
    down vote













    One thing I would like to change is the responsibilities of VerifyPalindrome(string word). Remember, your function should only check whether word is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:



    private static bool VerifyPalindrome(string word)

    bool compare = true;

    // ... computing code

    return compare;



    And in Main:



    if(VerifyPalindrome(word))

    Console.WriteLine("This is a Palindrome word");

    else

    Console.WriteLine("This is NOT a Palindrome word");



    The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare for whether a string is a palindrome or not and using breaks, which increases the complexity of your code, you can reduce the function to the following, with multiple returns:



    private static bool VerifyPalindrome(string word)

    int i = 0;
    while (i < (word.Length / 2) - 1)

    for (int j = word.Length - 1; j >= 0; j--)

    if (word[i] != word[j])
    return false;
    i++;


    return true;



    No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.






    share|improve this answer



























      up vote
      2
      down vote













      One thing I would like to change is the responsibilities of VerifyPalindrome(string word). Remember, your function should only check whether word is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:



      private static bool VerifyPalindrome(string word)

      bool compare = true;

      // ... computing code

      return compare;



      And in Main:



      if(VerifyPalindrome(word))

      Console.WriteLine("This is a Palindrome word");

      else

      Console.WriteLine("This is NOT a Palindrome word");



      The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare for whether a string is a palindrome or not and using breaks, which increases the complexity of your code, you can reduce the function to the following, with multiple returns:



      private static bool VerifyPalindrome(string word)

      int i = 0;
      while (i < (word.Length / 2) - 1)

      for (int j = word.Length - 1; j >= 0; j--)

      if (word[i] != word[j])
      return false;
      i++;


      return true;



      No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        One thing I would like to change is the responsibilities of VerifyPalindrome(string word). Remember, your function should only check whether word is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:



        private static bool VerifyPalindrome(string word)

        bool compare = true;

        // ... computing code

        return compare;



        And in Main:



        if(VerifyPalindrome(word))

        Console.WriteLine("This is a Palindrome word");

        else

        Console.WriteLine("This is NOT a Palindrome word");



        The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare for whether a string is a palindrome or not and using breaks, which increases the complexity of your code, you can reduce the function to the following, with multiple returns:



        private static bool VerifyPalindrome(string word)

        int i = 0;
        while (i < (word.Length / 2) - 1)

        for (int j = word.Length - 1; j >= 0; j--)

        if (word[i] != word[j])
        return false;
        i++;


        return true;



        No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.






        share|improve this answer















        One thing I would like to change is the responsibilities of VerifyPalindrome(string word). Remember, your function should only check whether word is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:



        private static bool VerifyPalindrome(string word)

        bool compare = true;

        // ... computing code

        return compare;



        And in Main:



        if(VerifyPalindrome(word))

        Console.WriteLine("This is a Palindrome word");

        else

        Console.WriteLine("This is NOT a Palindrome word");



        The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare for whether a string is a palindrome or not and using breaks, which increases the complexity of your code, you can reduce the function to the following, with multiple returns:



        private static bool VerifyPalindrome(string word)

        int i = 0;
        while (i < (word.Length / 2) - 1)

        for (int j = word.Length - 1; j >= 0; j--)

        if (word[i] != word[j])
        return false;
        i++;


        return true;



        No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Feb 24 at 14:24


























        answered Feb 23 at 23:20









        Arnav Borborah

        654119




        654119






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188234%2fpalindrome-algorithm%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