Great Tutorial but your program won't work if the given number is Negative, for eg: -121 or so....so in a while loop instead of (num > 0) use while(num != 0) and it will work for all the test cases
private boolean isPalindrome(String str) { String revStr = new StringBuffer(str).reverse().toString(); //can also use StringBuilder instead of StringBuffer return (revStr.equalsIgnoreCase(str)); }
Without reverse method String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); for (int i = length - 1; i >= 0; i--) reverse = reverse +original.charAt(i); if (original.equals(reverse)) System.out.println("The string is a palindrome."); else System.out.println("The string isn't a palindrome."); }
public static void StringRev(String string){ String t; t=string; StringBuffer reversestring = new StringBuffer(String.valueOf(string)).reverse(); System.out.println(reversestring); if(t.contentEquals(reversestring)) { System.out.println("It is a PALINDROME"); } else { System.out.println("It is NOT a PALINDROME"); }
@@sarojjha9243 A bit optimized code with less var : public static void RevString(String str) { StringBuilder rev = new StringBuilder(str).reverse(); if (str.contentEquals(rev)) System.out.println(str + " is Palindrome String."); else System.out.println(str + " is not Palindrome String."); }
Palindrome String program: public class PalindromeString { public static void isPalindromeString(String name) { String rev = " "; int length = name.length(); for (int i = length - 1; i >= 0; i--) { rev = rev + name.charAt(i); } System.out.println("The reverse of the name - " + rev); if (rev.equals(name)) { System.out.println(rev+ " - is a polindrome"); } else System.out.println(rev+ " - not a polindrome"); } public static void main(String[] args) { isPolindromeString("MADAM"); isPolindromeString("Madam"); } }
Hi Naveen, Just wanted to know, Can we write the above program in below way. Or what an interviewer can say if I give the below answer. Please suggest and let me know what is the issue in this. public class PalindromeNumber { public static void main(String[] args) {
int a = 252;
String b = String.valueOf(a);
StringBuffer sb = new StringBuffer(b); StringBuffer c = sb.reverse(); String d = c.toString();
if (b.equals(d)) { System.out.println("the number is palindrome number"); }
else { System.out.println("The number is not a palindrome number"); } } }
hi sir , how to write a code to separate number,alphabhaet, special character from a ... string input = (a24js7f.%^ output =ajsf 247 (.%^ this was asked in recent interview,please explain
An alternate approach - Single program for both integers and strings package largestNumber; import java.util.Scanner; public class Palindrome { public static int[] checkPalindrome(Object x) { int flag = 0; int a[] = new int[2]; String str = String.valueOf(x); int length = str.length(); if (length % 2 == 0) { for (int i = 0; i < (length / 2); i++) { if (str.toUpperCase().charAt(i) == str.toUpperCase().charAt(length - i - 1)) { flag++; } } } else { for (int i = 0; i < (int) (length / 2); i++) { if (str.toUpperCase().charAt(i) == str.toUpperCase().charAt(length - i - 1)) { flag++; } } } a[0] = length; a[1] = flag; return a; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("ENTER SOMETHING"); Object x = sc.nextLine();
System.out.println(x); int[] b = checkPalindrome(x); if (b[1] == (int) (b[0] / 2)) { System.out.println(x + " IS A PALINDROME"); } else { System.out.println(x + " IS NOT A PALINDROME"); } } }
Solution for String Palindrome public static void isPalindrome(String a){ String b = " "; int n = a.length(); for(int i = n-1; i>=0;i--){ b = b+a.charAt(i); } if(a.equalsIgnoreCase(b)){ System.out.Println("The enterd String is Palindrome); } else{ System.out.println("The enterd String is not a palindrome"); } } Call this into the Main Method and pass the input String...
if(rev.equals(str)) { return true; } return false; } public static void main(String[] args) { // TODO Auto-generated method stub
System.out.println("'radar' is a Palindrome: " + isPalindrome("radar")); System.out.println("'selenium' is a Palindrome: " + isPalindrome("selenium")); System.out.println("'level' is a Palindrome: " + isPalindrome("level")); } }
Program to check if a string is palidrome or not : public class PalindromeString { public static void isPalindromeString(String s) { System.out.println("Given string is :" + s); // reverse the given String String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not if (s.equals(reverse)) { System.out.println("a Palindrome string"); } else System.out.println("not a palindrome string"); } public static void main(String args[]) throws java.lang.Exception { isPalindromeString(" madam "); isPalindromeString(" malayalam "); isPalindromeString(" renuka "); isPalindromeString(" abbc "); } }
public class PalindromeClass { public void isPalindromString(String str1) { String str2= ""; int i = str1.length(); for(i =str1.length()-1;i>=0;i--) { str2 = str2+str1.charAt(i); } //System.out.println(str2); if (str1.equals(str2)) { System.out.println("This is a Palindrome String"); } else { System.out.println("This is not a Palindrome String"); } } public static void main(String[] args) { PalindromeClass obj =new PalindromeClass(); obj.isPalindromString("teet"); } }
package introduction; public class PalindromeCheck { public static void main(String[] args) { String input = "nitin"; int i = input.length(); char[] ar = input.toCharArray(); boolean b = isPallindrom(i, ar); if (b==true) { System.out.println("Mr. Saksham Bahati, The number youve entered is pallidrome"); } else { System.out.println("Mr. Saksham Bahati The number you've entered is not pallindrome"); } } public static boolean isPallindrom(int length, char y[]) { int j = length - 1; for (int i = 0; i < length/2; i++) { if (y[j]==y[i]) { j--; } else { return false; } } return true; } } Thanks @naveen
Great Tutorial but your program won't work if the given number is Negative, for eg: -121 or so....so in a while loop instead of (num > 0) use while(num != 0) and it will work for all the test cases
negative numbers are not palindrome number
sir i hope would u complete programming video series at least 50-60 program mainly from string & array
Thank you... U thought nicely.. Can u make more videos of stings, arrays, nd patterns..
private boolean isPalindrome(String str) {
String revStr = new StringBuffer(str).reverse().toString(); //can also use StringBuilder instead of StringBuffer
return (revStr.equalsIgnoreCase(str));
}
Hello , if you have time can you please cover Anagram , Duplicate Word , Permutation, Determined Largest Word please , Thank you
Without reverse method
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse +original.charAt(i);
if (original.equals(reverse))
System.out.println("The string is a palindrome.");
else
System.out.println("The string isn't a palindrome.");
}
public static void StringRev(String string){
String t;
t=string;
StringBuffer reversestring = new StringBuffer(String.valueOf(string)).reverse();
System.out.println(reversestring);
if(t.contentEquals(reversestring)) {
System.out.println("It is a PALINDROME");
}
else {
System.out.println("It is NOT a PALINDROME");
}
Good one.
Not working, it reaches all-time to else part and displaying not a palindrome.Someone please check the code
@@sarojjha9243 A bit optimized code with less var :
public static void RevString(String str) {
StringBuilder rev = new StringBuilder(str).reverse();
if (str.contentEquals(rev))
System.out.println(str + " is Palindrome String.");
else
System.out.println(str + " is not Palindrome String.");
}
I have one doubt: Is '1001' is a palindrome (Note the single quotes before answering)?It would be very nice if you can show the program using strings.
Palindrome String program:
public class PalindromeString {
public static void isPalindromeString(String name) {
String rev = " ";
int length = name.length();
for (int i = length - 1; i >= 0; i--) {
rev = rev + name.charAt(i);
}
System.out.println("The reverse of the name - " + rev);
if (rev.equals(name)) {
System.out.println(rev+ " - is a polindrome");
} else
System.out.println(rev+ " - not a polindrome");
}
public static void main(String[] args) {
isPolindromeString("MADAM");
isPolindromeString("Madam");
}
}
Hi Naveen, Just wanted to know, Can we write the above program in below way. Or what an interviewer can say if I give the below answer. Please suggest and let me know what is the issue in this.
public class PalindromeNumber {
public static void main(String[] args)
{
int a = 252;
String b = String.valueOf(a);
StringBuffer sb = new StringBuffer(b);
StringBuffer c = sb.reverse();
String d = c.toString();
if (b.equals(d))
{
System.out.println("the number is palindrome number");
}
else
{
System.out.println("The number is not a palindrome number");
}
}
}
public static void isPalindromeString(String name) {
String original=name;
int length=name.length();
String reverseString="";
System.out.println("Lenght of String:"+length);
for(int i=length-1;i>=0;i--) {
reverseString=reverseString+name.charAt(i);
System.out.println(reverseString);
}
System.out.println("Reversed: "+reverseString);
if(original.equals(reverseString)) {
System.out.println("Its a Palindrom String");
} else {
System.out.println("Not a Palindrom String");
}
}
StringBuffer sb = new StringBuffer("Tejas");
sb.append("Toley");
system.out.println(sb.reverse);
Hi thanks for this programming
i tried this code this is also working
public static void checkPalindrom(int x) {
int num = x;
ArrayList arr=new ArrayList();
while(num>0) {
arr.add(num%10);
num=num/10;
}
int c=0;
int i=0;
int j=arr.size()-1;
while(i
hi sir , how to write a code to separate number,alphabhaet, special character from a ...
string input = (a24js7f.%^
output =ajsf
247
(.%^
this was asked in recent interview,please explain
V expect more program videos
An alternate approach - Single program for both integers and strings
package largestNumber;
import java.util.Scanner;
public class Palindrome {
public static int[] checkPalindrome(Object x) {
int flag = 0;
int a[] = new int[2];
String str = String.valueOf(x);
int length = str.length();
if (length % 2 == 0) {
for (int i = 0; i < (length / 2); i++) {
if (str.toUpperCase().charAt(i) == str.toUpperCase().charAt(length - i - 1)) {
flag++;
}
}
}
else {
for (int i = 0; i < (int) (length / 2); i++) {
if (str.toUpperCase().charAt(i) == str.toUpperCase().charAt(length - i - 1)) {
flag++;
}
}
}
a[0] = length;
a[1] = flag;
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER SOMETHING");
Object x = sc.nextLine();
System.out.println(x);
int[] b = checkPalindrome(x);
if (b[1] == (int) (b[0] / 2)) {
System.out.println(x + " IS A PALINDROME");
}
else {
System.out.println(x + " IS NOT A PALINDROME");
}
}
}
Great tutor
Glad you liked it
Solution for String Palindrome
public static void isPalindrome(String a){
String b = " ";
int n = a.length();
for(int i = n-1; i>=0;i--){
b = b+a.charAt(i);
}
if(a.equalsIgnoreCase(b)){
System.out.Println("The enterd String is Palindrome);
}
else{
System.out.println("The enterd String is not a palindrome");
}
}
Call this into the Main Method and pass the input String...
If we give any palindrome number starting with 0 this logic does not work example 012210
Thanks
To check if a given String is Palindrome
public class PalindromeString {
public static boolean isPalindrome(String str) {
String rev = "";
for(int i = str.length()-1; i>=0; i--) {
rev = rev+str.charAt(i);
}
if(rev.equals(str)) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("'radar' is a Palindrome: " + isPalindrome("radar"));
System.out.println("'selenium' is a Palindrome: " + isPalindrome("selenium"));
System.out.println("'level' is a Palindrome: " + isPalindrome("level"));
}
}
Program to check if a string is palidrome or not :
public class PalindromeString {
public static void isPalindromeString(String s) {
System.out.println("Given string is :" + s);
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse)) {
System.out.println("a Palindrome string");
}
else
System.out.println("not a palindrome string");
}
public static void main(String args[])
throws java.lang.Exception
{
isPalindromeString(" madam ");
isPalindromeString(" malayalam ");
isPalindromeString(" renuka ");
isPalindromeString(" abbc ");
}
}
String malayalam
How to remember the logic man
public class PalindromeClass {
public void isPalindromString(String str1) {
String str2= "";
int i = str1.length();
for(i =str1.length()-1;i>=0;i--)
{
str2 = str2+str1.charAt(i);
}
//System.out.println(str2);
if (str1.equals(str2))
{
System.out.println("This is a Palindrome String");
}
else
{
System.out.println("This is not a Palindrome String");
}
}
public static void main(String[] args) {
PalindromeClass obj =new PalindromeClass();
obj.isPalindromString("teet");
}
}
String pop
//String Reverse
//String palindrome check
//Case sensitive handle
public class StringReverse {
public static void main(String[] args) {
ReverseString("kuuk");
}
public static void ReverseString(String string) {
string=string.toUpperCase();
String rev="";
//System.out.println(string.length());
//System.out.println(string);
for(int i=string.length()-1;i>=0;i--) {
rev=rev+string.charAt(i);
}
System.out.println("Reverse String is :"+rev);
if(string.equals(rev)) {
System.out.println("String is Palindrome: "+rev);
}
else {
System.out.println("String is not palindrome: "+rev);
}
}
package introduction;
public class PalindromeCheck {
public static void main(String[] args) {
String input = "nitin";
int i = input.length();
char[] ar = input.toCharArray();
boolean b = isPallindrom(i, ar);
if (b==true) {
System.out.println("Mr. Saksham Bahati, The number youve entered is pallidrome");
}
else {
System.out.println("Mr. Saksham Bahati The number you've entered is not pallindrome");
}
}
public static boolean isPallindrom(int length, char y[]) {
int j = length - 1;
for (int i = 0; i < length/2; i++) {
if (y[j]==y[i]) {
j--;
}
else {
return false;
}
}
return true;
}
}
Thanks @naveen
Thanks