Sir , Your teaching method is very easy,quick and understandable to everyone. We are lucky to have you sir Thank you so much
What about reversing the string and then comparing it with the real string?
I appreciate the time you have dedicated to do these tutorials. I am currently learning Java with you and I'm enjoying the journey! Thank you kindly for sharing your knowledge with people all around the world!
I remember 10 years ago my professor teach this type of loop with a lot of parameters, but in this time for me is not was clear, I want to sincere with you, is the first time that I understand you very clear how to use the loop for with more that three parameters, great explanation, regards from PerĂº.
int length=s.length(); int c=length-1; int i=0; boolean flag=false; while(c>=0) { if(s.charAt(c)==s.charAt(i)) { c=c-1; i++; flag=true; } else { flag=false; break; } } System.out.println(flag?s+" is a palindrome":s+" is not a palindrome");
String str = scan.nextLine(); boolean flag = true; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != str.charAt((str.length() - 1 - i))) { flag = false; break; } } if (flag == true) System.out.println(str + " is polindrom"); else System.out.println(str + " is not polindrom");
I applied the manual string input, break statement and the boolean concept: import java.util.Scanner; public class LoopEx10_Palindrome { public static void main(String[] args) { System.out.print("Enter a string: "); Scanner scr=new Scanner(System.in); String s=scr.nextLine(); String reverse=""; int len=s.length();int i; boolean c=true; for(i=len-1;i>=0;i--) reverse=reverse+s.charAt(i); for(i=0;i<len;i++) { if(s.charAt(i)!=reverse.charAt(i)) { c=false; break; } } System.out.println((c==true)?"Palindrom":"NOT"); } }
String str = scan.nextLine(); String checkPal = ""; for (int i = str.length()-1; i >= str.length()/2; i--) checkPal += str.charAt(i); System.out.println(str.startsWith(checkPal)? str + " is a Palindrom" : str + " is Not a Palindrom");
Thanks sir
I freaked out but worked like this too in a messed up way :: public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("enter any string : "); String nam = input.nextLine(); //find if the string is palindrome or not int j=0; boolean changer=true; for(int i=nam.length()-1;i>=0;i--,j++) { if(nam.charAt(i)!=nam.charAt(j)) { changer=false; System.out.println("not palindrome"); break; } changer=true; } if(changer) { System.out.println("palindrome"); } }
import java.util.Scanner; public class M_52 { public static void main(String []args) { Scanner input = new Scanner(System.in); String str = "abba"; int i = 0; int j = str.length() - 1; while(str.charAt(i) == str.charAt(j)) { if(i > j) { System.out.println("Palindrome"); break; } i++; j--; } if(i < j) System.out.println("Non-palindrome"); } }
Please Try to post video's faster. It will be more helpful to us.
Here is my solution: Scanner s = new Scanner(System.in); System.out.print("enter the string : "); String str = s.nextLine(); Boolean isPalindrome = true; int length = str.length(); for (int i = 0; i <= str.length()/2-1; i++ ) { if (str.charAt(i) != str.charAt(length - 1)) isPalindrome = false; length = length-1; } System.out.println("This is" + (isPalindrome == true ? "Palindrome" : " Not Palindrome"));
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.next(); boolean isPalindrome = true; for(int i = 0; i < word.length(); i++){ if(word.charAt(i) != word.charAt(word.length() - i - 1)){ isPalindrome = false; } } System.out.println(isPalindrome); }
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a string: "); String s = input.nextLine(); for (int i = 0, j = s.length() - 1; i < j; i++, j--) { if (s.charAt(i) == s.charAt(j)) System.out.println(s + " is a Palindromes string!"); else System.out.println(s + " is not a Palindromes string!"); break; } } }
Instead of writing this much of tough code I think we can dolike this initially we have declared and initialized a string to a variable and then we can reverse the string and we are going to check if our string is equal to the reversed string using if statements Let me know we can do in this way or not
public class Main { public static void main(String[] args) { System.out.print("enter the string: "); Scanner s = new Scanner(System.in); String s1 = s.nextLine(); String s2 = ""; for (int i = s1.length() - 1; i >= 0; i--) { s2 += s1.charAt(i); } if (s2.equals(s1)) System.out.print("palindrome " + "s1:" + s1 + " s2:" + s2); } } MY VERSION
// palindrome number Scanner obj=new Scanner(System.in); int n=obj.nextInt(); String n2=(n+""); String rev=""; for(;n>0;){ rev=rev+(n%10); n=n/10; } if(n2.equals(rev)){ System.out.println("Its a palindrome"); } else { System.out.println("Its not a palindrome"); }
I have dbt Sir I have a string ex: s ="DaD" Then I reversed, it become rev="DaD" Can I do like the following If(s == rev) Sout("Palindrome"); Else Sout("Not a Palindrome"); If I can't do like this way ,why??? What is happening?? When I did like this it always giving not a Palindrome Even I had given the palindrome
@ishanpatni2684