string palindrome in java without using inbuilt function code example
Example 1: String palindrome in java without using reverse method
import java.util.Scanner;
public class StringPalindromeDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter string to check palindrome: ");
String strInput = sc.nextLine();
char[] chString = strInput.toCharArray();
String strReverse = "";
for(int a = chString.length - 1; a >= 0; a--)
{
strReverse = strReverse + chString[a];
}
System.out.println("Given string: " + strInput);
System.out.println("Reverse String: " + strReverse);
if(strInput.equals(strReverse))
{
System.out.println("string is palindrome.");
}
else
{
System.out.println("string is not palindrome.");
}
}
}
Example 2: String palindrome in java using stringbuffer
public class PalindromeUsingStringBuffer
{
public static void main(String[] args)
{
String strInput = "nayan";
StringBuffer sb = new StringBuffer(strInput);
sb.reverse();
String str = sb.toString();
if(strInput.equals(str))
{
System.out.println("string is palindrome.");
}
else
{
System.out.println("string is not palindrome.");
}
}
}