Length():-This method is a predefined method of String class present in java. Lang package. It is used to determine the length of string and return type of this method is int type.
Syntax: public int length()
getBytes():-This method is a predefined method of String class present in java .lang package.It is used to ,convert a String into an array of bytes.
Syntax: public byte[] getBytes()
Algorithm for palindrome or not:-
step 1: Read str
step 2: Set size = str.length()
step 3: Create a byte array reference b[]
step 4: b[] = str getBytes()
step 5: create d byte array b1[] of size
step 6: initialize i=size-1
step 7: repeat through step-10 while (i>=0)
step 8: set x=size-(i+ 1)
step 9: b1[x]=b[i]
step 10: i=i-i
step 11: set count=0
step 12: reset i= 0
step 13: repeat through step-15 while (i < size)
step 14: if (b1[i]==b[i]) then count = count+1
step 15: i=i+ 1
step 16: if count==size then print “string is palindrome”
else print “string is not a palindrome”
step 17: Exit
Here is the Java Example for palindrome or not:
import java.util.Scanner; public class PalindromeorNot { public static int count=0; public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.print("Enter the String : "); String str=s.nextLine(); int size=str.length(); byte b[]=str.getBytes(); byte bl[]=new byte[size]; for(int i=size-1;i>=0;i--) { int x=size-(i+1); bl[x]=b[i]; } for(int i=0;i<size;i++) { if(bl[i]==b[i]) { count++; } } if(count==size) { System.out.println(str+" is a Pallindrome."); } else { System.out.println(str+" is not a pallindrome."); } } }