CS1020 Palindrome Lab 0 Ex 3

Link to the question – Palindrome Lab 0 Ex 3

import java.util.*;

public class Palindrome {
	/* use this method to check whether the string is palindrome word or not
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public static boolean isPalindrome(String word) {
		int length = word.length();
		String reverse = "";
		for(int i = length - 1; i >= 0; i--)
		{
			reverse = reverse + word.charAt(i);
		}
		if(word.equals(reverse))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static void main(String[] args) {
		// declare the necessary variables


		// declare a Scanner object to read input


		// read input and process them accordingly
		Scanner scan = new Scanner(System.in);
		String input1 = scan.next();
		String input2 = scan.next();
		if(input1.length() == input2.length())
		{
		String cat = input1 + input2;
		boolean checkPalindrome = isPalindrome(cat);
		// simulate the problem


		// output the result
		if(checkPalindrome == true)
		{
			System.out.println("YES");
		}
		else
		{
			System.out.println("NO");
		}
		}
	}
}
Advertisements
This entry was posted in NUS, SoC and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s