JAVA STRING BYTE ARRAY OBJECT
JAVA STRING BYTE ARRAY OBJECT
/*
6. WAP in Java to print
the palindrome word from a sentence.
*/
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString
{
static void cal(String input)
{
//getBytes() method to convert string
//into bytes[]
byte strAsByteArray[]=input.getBytes();
byte result[]=new byte[strAsByteArray.length];
//store result in reverse order
// into the result byte[]
for (int i=0; i< strAsByteArray.length; i++)
{
result[i]=strAsByteArray[strAsByteArray.length-i-1];
}
String n=new String(result);
if(n.equals(input))
System.out.print("\n "+n);
}
public static void main(String[] args) {
String input;
Scanner sc=new Scanner (System.in);
System.out.print("\nInput: ");
input=sc.nextLine();
int i=0,p=0;
int len=input.length();
System.out.print("\n Palindrome Words are: ");
while(i<len)
{
char n=input.charAt(i);
String s=Character.toString(n);
if (i==len-1)
{
//System.out.print("\n "+input.substring(p,i+1));
ReverseString.cal(input.substring(p,i+1));
}
else if (s.equals(" "))
{
//System.out.print("\n "+input.substring(p,i));
ReverseString.cal(input.substring(p,i));
p=i+1;
}
i++;
}
}
}
Comments
Post a Comment