Palindrome checker for Java












0















public static void main(String args) {
Scanner in = new Scanner(System.in);
String word;
String c;
int x, count, count1;
System.out.println("Please enter a word:");
word=in.nextLine();
x=word.length();
count1=x;
for(count=0;count<x;count++) {
c=word.substring((count1)-1,count1);
System.out.print(c);
count1--;
}`
}


Hi everyone, before I made this code that printed out a word backwards but now I am wondering how I can turn this into a palindrome checker but I am confused because the final product which is actually a word made out of a bunch of String c's put together, so I don't know how to compare it back to the original word that was entered. Help?










share|improve this question




















  • 1





    You can add strings together. Start with empty one and add c.

    – Michael Butscher
    Nov 14 '18 at 23:16













  • Possible duplicate of Check string for palindrome

    – Graph Theory
    Nov 15 '18 at 1:30











  • its not a duplicate because I want to know how it can be done using my code.

    – Shrey Varma
    Nov 16 '18 at 5:20
















0















public static void main(String args) {
Scanner in = new Scanner(System.in);
String word;
String c;
int x, count, count1;
System.out.println("Please enter a word:");
word=in.nextLine();
x=word.length();
count1=x;
for(count=0;count<x;count++) {
c=word.substring((count1)-1,count1);
System.out.print(c);
count1--;
}`
}


Hi everyone, before I made this code that printed out a word backwards but now I am wondering how I can turn this into a palindrome checker but I am confused because the final product which is actually a word made out of a bunch of String c's put together, so I don't know how to compare it back to the original word that was entered. Help?










share|improve this question




















  • 1





    You can add strings together. Start with empty one and add c.

    – Michael Butscher
    Nov 14 '18 at 23:16













  • Possible duplicate of Check string for palindrome

    – Graph Theory
    Nov 15 '18 at 1:30











  • its not a duplicate because I want to know how it can be done using my code.

    – Shrey Varma
    Nov 16 '18 at 5:20














0












0








0








public static void main(String args) {
Scanner in = new Scanner(System.in);
String word;
String c;
int x, count, count1;
System.out.println("Please enter a word:");
word=in.nextLine();
x=word.length();
count1=x;
for(count=0;count<x;count++) {
c=word.substring((count1)-1,count1);
System.out.print(c);
count1--;
}`
}


Hi everyone, before I made this code that printed out a word backwards but now I am wondering how I can turn this into a palindrome checker but I am confused because the final product which is actually a word made out of a bunch of String c's put together, so I don't know how to compare it back to the original word that was entered. Help?










share|improve this question
















public static void main(String args) {
Scanner in = new Scanner(System.in);
String word;
String c;
int x, count, count1;
System.out.println("Please enter a word:");
word=in.nextLine();
x=word.length();
count1=x;
for(count=0;count<x;count++) {
c=word.substring((count1)-1,count1);
System.out.print(c);
count1--;
}`
}


Hi everyone, before I made this code that printed out a word backwards but now I am wondering how I can turn this into a palindrome checker but I am confused because the final product which is actually a word made out of a bunch of String c's put together, so I don't know how to compare it back to the original word that was entered. Help?







java palindrome






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 23:24









syntagma

12.9k1249106




12.9k1249106










asked Nov 14 '18 at 23:12









Shrey VarmaShrey Varma

1




1








  • 1





    You can add strings together. Start with empty one and add c.

    – Michael Butscher
    Nov 14 '18 at 23:16













  • Possible duplicate of Check string for palindrome

    – Graph Theory
    Nov 15 '18 at 1:30











  • its not a duplicate because I want to know how it can be done using my code.

    – Shrey Varma
    Nov 16 '18 at 5:20














  • 1





    You can add strings together. Start with empty one and add c.

    – Michael Butscher
    Nov 14 '18 at 23:16













  • Possible duplicate of Check string for palindrome

    – Graph Theory
    Nov 15 '18 at 1:30











  • its not a duplicate because I want to know how it can be done using my code.

    – Shrey Varma
    Nov 16 '18 at 5:20








1




1





You can add strings together. Start with empty one and add c.

– Michael Butscher
Nov 14 '18 at 23:16







You can add strings together. Start with empty one and add c.

– Michael Butscher
Nov 14 '18 at 23:16















Possible duplicate of Check string for palindrome

– Graph Theory
Nov 15 '18 at 1:30





Possible duplicate of Check string for palindrome

– Graph Theory
Nov 15 '18 at 1:30













its not a duplicate because I want to know how it can be done using my code.

– Shrey Varma
Nov 16 '18 at 5:20





its not a duplicate because I want to know how it can be done using my code.

– Shrey Varma
Nov 16 '18 at 5:20












2 Answers
2






active

oldest

votes


















0














Start with a method signature and a value that you will return from it:



private boolean isPalindrome(String word1, String word2) {
boolean isPalindrome = true; // let's initiate it to true
// ...
return isPalindrome;
}


Then, you may use what you already have (I commented parts that are not needed:



x=word.length();
for(count=0;count<x;count++) {
// c=word.substring((count1)-1,count1);
// you can use word.charAt(count); instead
// System.out.print(c);
//count1--;
}`


Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.



You may also want to check first if both strings have same length.






share|improve this answer































    0














    I see 2 possibilities to turn this in a palindrom-checker (i changed your existing code a little bit)





    1. compare the chars of the first half of the string to the second half, something like this:



      boolean isPalindrom = true;

      for (int i= 0; i < (word.length()-1)/2; i++) {
      if(word.charAt(i) != word.charAt(word.length()-1-i))
      {
      isPalindrom = false;
      }
      }

      System.out.println(word + " is a palindrom: " + isPalindrom);



    or





    1. use your reversed String, save it to another String-variable and then check it it is equal to your word.
      Something like this:



      public static void main(String args) {
      Scanner in = new Scanner(System.in);
      String word;

      System.out.println("Please enter a word:");
      word = in.nextLine();

      String reverseString = "";

      for (int i = 0; i < word.length(); i++) {
      reverseString = reverseString + word.charAt(word.length()-1-i);;
      }

      System.out.println("original: " + word + ", reversed: " + reverseString);

      boolean isPalindrom = word.equals(reverseString);

      System.out.println(word + " is a palindrom: " + isPalindrom);
      }



    Maybe this is also helpful for you: Check string for palindrome






    share|improve this answer























      Your Answer






      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: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310152%2fpalindrome-checker-for-java%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Start with a method signature and a value that you will return from it:



      private boolean isPalindrome(String word1, String word2) {
      boolean isPalindrome = true; // let's initiate it to true
      // ...
      return isPalindrome;
      }


      Then, you may use what you already have (I commented parts that are not needed:



      x=word.length();
      for(count=0;count<x;count++) {
      // c=word.substring((count1)-1,count1);
      // you can use word.charAt(count); instead
      // System.out.print(c);
      //count1--;
      }`


      Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.



      You may also want to check first if both strings have same length.






      share|improve this answer




























        0














        Start with a method signature and a value that you will return from it:



        private boolean isPalindrome(String word1, String word2) {
        boolean isPalindrome = true; // let's initiate it to true
        // ...
        return isPalindrome;
        }


        Then, you may use what you already have (I commented parts that are not needed:



        x=word.length();
        for(count=0;count<x;count++) {
        // c=word.substring((count1)-1,count1);
        // you can use word.charAt(count); instead
        // System.out.print(c);
        //count1--;
        }`


        Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.



        You may also want to check first if both strings have same length.






        share|improve this answer


























          0












          0








          0







          Start with a method signature and a value that you will return from it:



          private boolean isPalindrome(String word1, String word2) {
          boolean isPalindrome = true; // let's initiate it to true
          // ...
          return isPalindrome;
          }


          Then, you may use what you already have (I commented parts that are not needed:



          x=word.length();
          for(count=0;count<x;count++) {
          // c=word.substring((count1)-1,count1);
          // you can use word.charAt(count); instead
          // System.out.print(c);
          //count1--;
          }`


          Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.



          You may also want to check first if both strings have same length.






          share|improve this answer













          Start with a method signature and a value that you will return from it:



          private boolean isPalindrome(String word1, String word2) {
          boolean isPalindrome = true; // let's initiate it to true
          // ...
          return isPalindrome;
          }


          Then, you may use what you already have (I commented parts that are not needed:



          x=word.length();
          for(count=0;count<x;count++) {
          // c=word.substring((count1)-1,count1);
          // you can use word.charAt(count); instead
          // System.out.print(c);
          //count1--;
          }`


          Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.



          You may also want to check first if both strings have same length.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 23:29









          syntagmasyntagma

          12.9k1249106




          12.9k1249106

























              0














              I see 2 possibilities to turn this in a palindrom-checker (i changed your existing code a little bit)





              1. compare the chars of the first half of the string to the second half, something like this:



                boolean isPalindrom = true;

                for (int i= 0; i < (word.length()-1)/2; i++) {
                if(word.charAt(i) != word.charAt(word.length()-1-i))
                {
                isPalindrom = false;
                }
                }

                System.out.println(word + " is a palindrom: " + isPalindrom);



              or





              1. use your reversed String, save it to another String-variable and then check it it is equal to your word.
                Something like this:



                public static void main(String args) {
                Scanner in = new Scanner(System.in);
                String word;

                System.out.println("Please enter a word:");
                word = in.nextLine();

                String reverseString = "";

                for (int i = 0; i < word.length(); i++) {
                reverseString = reverseString + word.charAt(word.length()-1-i);;
                }

                System.out.println("original: " + word + ", reversed: " + reverseString);

                boolean isPalindrom = word.equals(reverseString);

                System.out.println(word + " is a palindrom: " + isPalindrom);
                }



              Maybe this is also helpful for you: Check string for palindrome






              share|improve this answer




























                0














                I see 2 possibilities to turn this in a palindrom-checker (i changed your existing code a little bit)





                1. compare the chars of the first half of the string to the second half, something like this:



                  boolean isPalindrom = true;

                  for (int i= 0; i < (word.length()-1)/2; i++) {
                  if(word.charAt(i) != word.charAt(word.length()-1-i))
                  {
                  isPalindrom = false;
                  }
                  }

                  System.out.println(word + " is a palindrom: " + isPalindrom);



                or





                1. use your reversed String, save it to another String-variable and then check it it is equal to your word.
                  Something like this:



                  public static void main(String args) {
                  Scanner in = new Scanner(System.in);
                  String word;

                  System.out.println("Please enter a word:");
                  word = in.nextLine();

                  String reverseString = "";

                  for (int i = 0; i < word.length(); i++) {
                  reverseString = reverseString + word.charAt(word.length()-1-i);;
                  }

                  System.out.println("original: " + word + ", reversed: " + reverseString);

                  boolean isPalindrom = word.equals(reverseString);

                  System.out.println(word + " is a palindrom: " + isPalindrom);
                  }



                Maybe this is also helpful for you: Check string for palindrome






                share|improve this answer


























                  0












                  0








                  0







                  I see 2 possibilities to turn this in a palindrom-checker (i changed your existing code a little bit)





                  1. compare the chars of the first half of the string to the second half, something like this:



                    boolean isPalindrom = true;

                    for (int i= 0; i < (word.length()-1)/2; i++) {
                    if(word.charAt(i) != word.charAt(word.length()-1-i))
                    {
                    isPalindrom = false;
                    }
                    }

                    System.out.println(word + " is a palindrom: " + isPalindrom);



                  or





                  1. use your reversed String, save it to another String-variable and then check it it is equal to your word.
                    Something like this:



                    public static void main(String args) {
                    Scanner in = new Scanner(System.in);
                    String word;

                    System.out.println("Please enter a word:");
                    word = in.nextLine();

                    String reverseString = "";

                    for (int i = 0; i < word.length(); i++) {
                    reverseString = reverseString + word.charAt(word.length()-1-i);;
                    }

                    System.out.println("original: " + word + ", reversed: " + reverseString);

                    boolean isPalindrom = word.equals(reverseString);

                    System.out.println(word + " is a palindrom: " + isPalindrom);
                    }



                  Maybe this is also helpful for you: Check string for palindrome






                  share|improve this answer













                  I see 2 possibilities to turn this in a palindrom-checker (i changed your existing code a little bit)





                  1. compare the chars of the first half of the string to the second half, something like this:



                    boolean isPalindrom = true;

                    for (int i= 0; i < (word.length()-1)/2; i++) {
                    if(word.charAt(i) != word.charAt(word.length()-1-i))
                    {
                    isPalindrom = false;
                    }
                    }

                    System.out.println(word + " is a palindrom: " + isPalindrom);



                  or





                  1. use your reversed String, save it to another String-variable and then check it it is equal to your word.
                    Something like this:



                    public static void main(String args) {
                    Scanner in = new Scanner(System.in);
                    String word;

                    System.out.println("Please enter a word:");
                    word = in.nextLine();

                    String reverseString = "";

                    for (int i = 0; i < word.length(); i++) {
                    reverseString = reverseString + word.charAt(word.length()-1-i);;
                    }

                    System.out.println("original: " + word + ", reversed: " + reverseString);

                    boolean isPalindrom = word.equals(reverseString);

                    System.out.println(word + " is a palindrom: " + isPalindrom);
                    }



                  Maybe this is also helpful for you: Check string for palindrome







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 23:43









                  csalmhofcsalmhof

                  11617




                  11617






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310152%2fpalindrome-checker-for-java%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine