tag:blogger.com,1999:blog-8712770457197348465.post6038467489880608518..comments2024-03-29T05:54:46.190-07:00Comments on Javarevisited: 2 Ways to check If String is Palindrome in Java? Recursion and Loop Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8712770457197348465.post-2401294435054061042019-09-18T08:15:10.830-07:002019-09-18T08:15:10.830-07:00Scanner input = new Scanner(System.in);
St...Scanner input = new Scanner(System.in);<br /> String z = null;<br /> z = input.nextLine();<br /> int StringLenght = z.length();<br /> <br /> for(int i = 0; i < StringLenght/2; i++){<br /> if(z.charAt(i) == z.charAt(StringLenght-1-i)){<br /> System.out.println("The string is Palindrome:");<br /> }<br />Anonymoushttps://www.blogger.com/profile/01986416096338833539noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72293921873333264552017-05-04T17:31:41.292-07:002017-05-04T17:31:41.292-07:00That makes much more sense in the second version, ...That makes much more sense in the second version, since you are checking the reverse of the string equalling the original string. Or if you just compared the nth char with the (length-1)th char. like, first compared to last, second compared to second-last, all the way to the middle. <br /><br />Unless you just missed something in your first example. A palindrome is a word that reads the same AnthXhttps://www.blogger.com/profile/05348690849394693516noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21353327070344652382017-05-02T10:39:34.091-07:002017-05-02T10:39:34.091-07:00Anthony, There is no need to check the whole strin...Anthony, There is no need to check the whole string using loop if first and last character of the string is same then your string is palindrome , e.g. "madam" is a palindrome string. Here is another logic but this has more time and space complexity.<br />System.out.println("Enter the string");<br /> String str=new Scanner(System.in).nextLine();<br /> String Result="&Sahil Bhartihttps://www.blogger.com/profile/10621919959007099385noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13669343037014107242017-05-01T21:50:51.964-07:002017-05-01T21:50:51.964-07:00how would that check the whole string Sahil? It lo...how would that check the whole string Sahil? It looks like your code fragment would only check the first and last chacters.AnthXhttps://www.blogger.com/profile/05348690849394693516noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2262491789949894422017-02-23T02:59:11.886-08:002017-02-23T02:59:11.886-08:00Here is solution in a simple way without using loo...Here is solution in a simple way without using loop.<br />We are checking only first and last index.<br />String str="radar";<br /> int j=str.length()-1;<br /> if(str.charAt(0)==str.charAt(j))<br /> System.out.println("String is Palindrome");<br /> else<br /> System.out.println("String is not Palindrome");Sahil Bhartihttps://www.blogger.com/profile/10621919959007099385noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32897400256864071272016-10-17T21:53:50.321-07:002016-10-17T21:53:50.321-07:00CheckPalindrome method will not work.You have give...CheckPalindrome method will not work.You have given <br /><br />new StringBuilder(text);<br /><br />and later appending the reverse of the string to the same stringbuilder sb.<br /><br />So if my string is 676 the reversed string as per your prog will be 676676. How is it working for you? The Stringbuilder line should be <br /><br />Stringbuilder sb = new StringBuilder();<br /><br /><br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86296874847248547772015-07-11T04:21:04.505-07:002015-07-11T04:21:04.505-07:00@Suan, that's indeed a good solution without u...@Suan, that's indeed a good solution without using recursion. Best part of this is that it doesn't need to reverse the whole string to check if given String is palindrome, as soon as you find one mismatched character you can say that its not palindrome. Great solution ..javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2559884705308915812015-07-11T04:16:44.358-07:002015-07-11T04:16:44.358-07:00Hello @Hybrid Static, here we are extracting the f...Hello @Hybrid Static, here we are extracting the first character and passing rest of the String to method itself (the recursive call). After that we are adding that first character at then end of what reverse() return. So if your string is "abcd" then str.substring(1) = bcd and str.charAt(0) = a.<br /><br />When recursion will complete and things will start rolling back e.g. when inputjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38745399207487346252015-07-01T07:05:35.877-07:002015-07-01T07:05:35.877-07:00Hey dear Paul nice article :)
I would like to expl...Hey dear Paul nice article :)<br />I would like to explain me this line :<br /> return reverse(str.substring(1)) + str.charAt(0);Anonymoushttps://www.blogger.com/profile/03178441881572855932noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76082710961003522942015-06-29T18:32:11.618-07:002015-06-29T18:32:11.618-07:00Hi! Great article. :)
Here's an alternative s...Hi! Great article. :)<br /><br />Here's an alternative solution (if you're interested):-<br /><br />String a = "wooow";<br />int j = a.length() - 1;<br />for (int i = 0; i < a.length() / 2; i++) {<br /> if (a.charAt(i) != a.charAt(j)) {<br /> System.out.println("NOT PALINDROME!");<br /> break;<br /> }<br /> j--;<br />}<br /><br />It's a O(Suanhttps://www.blogger.com/profile/00192998944313191986noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35525082954622061742015-06-28T02:43:38.037-07:002015-06-28T02:43:38.037-07:003rd way: http://stackoverflow.com/questions/756933...3rd way: http://stackoverflow.com/questions/7569335/reverse-a-string-in-java<br /><br />new StringBuilder(hi).reverse().toString()Anonymousnoreply@blogger.com