Translate

Monday, June 6, 2016

Write a program to check whether a string is palindrome or not.



package com.javahelp.stringprog;

import java.util.Scanner;

public class StringPalindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine(); // enter the string
int j=s.length()-1;  // assign the length of string to variable
int i=0;
for(; i<s.length(); i++)  
{
if(i<=j)  
{
if(s.charAt(i)==s.charAt(j)) // compare the string character from beginning and from the end
{
j--;
}
else
{
System.out.println("String is not palindrome");
break;
}
}
}
if(i>=j)
{
System.out.println("String is palindrome");
}
}
}



No comments:

Post a Comment