Monday 30 May 2016

Java program to check whether a given string is palindrome or not without taking reverse.

import java.util.*;
public class StringPalindrome {

public static void main(String[] args)
{
String str;
int l,c=0;
Scanner s= new Scanner(System.in);
System.out.println("enter a string");
str= s.nextLine();
l=str.length();
for(int i=0;i<(l-1)/2; i++)
{
if(str.charAt(i)==str.charAt(l-1-i))
{
continue;
}
else
{
c++;
}
}
if(c==0)
System.out.println("the given string is palindrome");
else
System.out.println("the given string is not palindrome");

}

}

Output
1st Run:
enter a string
katak
the given string is palindrome

2nd Run:
enter a string
Silan
the given string is not palindrome

No comments:

Post a Comment