1) To check weather a given string is palindrome or not in java.
Palindrome string a string which is same while reading from forward to backword or backword to forword (Ex. madam,lo,pop,rubber)
-To do that that there is very simple logic in java.
1)We need to take a length of the string
2)Reverse a string
3)compare both the string
import java.util.Scanner; public class Reversestring { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("enter the string: "); String str=sc.next(); int length=str.length(); String s2=""; for(int i=length-1;i>=0;i--) s2=s2+str.charAt(i); if(s2.equalsIgnoreCase(str)) { System.out.println("palindrome"); } else { System.out.println("not palindrom"); } } }
No comments:
Post a Comment