Saturday, 20 January 2018

Write a Java Program to Check String is Palindrome or not

import java.util.Scanner;

public class PalindromeString {
     public static void main(String[] args) {
           System.out.print("Enter any string to check Palindrome : ");
           Scanner sc = new Scanner(System.in);
           String str = sc.nextLine();
           String reverseString = "";
           for(int i = str.length()-1 ; i>=0;i--){
               
                reverseString = reverseString+Character.toString(str.charAt(i));
           }
           if(reverseString.equals(str)){
                System.out.println("Enter string '"+str+"' is palindrome.");
           }else{
                System.out.println("Enter string '"+str+"' is not palindrome.");
           }
     }


}


Output: 

Enter any string to check Palindrome : madam
Enter string 'madam' is palindrome.

Enter any string to check Palindrome : Tutorial4u
Enter string 'Tutorial4u' is not palindrome.

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...