Saturday, 15 June 2013

Palindrome(Palingram) and Anagram Java Programs



Palindrome(Palingram) and Anagram Test


PALINDROME


A palindrome is a word or sentence that reads the same forward as it does backward. Example:- racecar, civic, madam, level. Palindromes are a type of palingram called letter palingrams. A palingram is a sentence in which the letters, syllables, or words read the same backward as they do forward.
A Java program to find the given word is a palindrome or not.

packagecom.gaurav.coreprograms;

importjava.util.Scanner;
importjava.util.logging.Logger;

/**
 * This program is responsible to find the given String is a palindrome or not.
 *
 * @author KUMAR GAURAV
 *
 */
public classPalindromeTest {

            privatefinal static Logger logger = Logger.getLogger(PalindromeTest.class
                                    .getName());

            /**
             * This is a way to find the given word is a palindrome.
             * */
            publicboolean checkPalindrome(String inputWord) {
                        try{
                                    char[] charArray = inputWord.toCharArray();
                                    intarrayLength = charArray.length - 1;
                                    intmiddleOfArray = Math.round(charArray.length / 2);

                                    for(int i = arrayLength; i >= middleOfArray; i--) {
                                                charleftPosition = charArray[arrayLength - i];
                                                charrightPosition = charArray[i];
                                                if(leftPosition != rightPosition)
                                                            returnfalse;
                                    }
                        } catch (Exception e) {
                                    logger.severe("ERROR:-" + e.getMessage());
                        }
                        returntrue;
            }

            /**
             * This is another way to find the given word is a palindrome.
             * */
            publicString getPalindrome(String strPalindrome) {
                        String palindromeStatus = "";
                        if(strPalindrome != null) {
                                    StringBuilder strBuilder1 = new StringBuilder(strPalindrome);
                                    StringBuilder strBuilder2 = new StringBuilder(strBuilder1);
                                    strBuilder1.reverse();
                                    logger.info("ACTULA GIVEN STRING IS:->" + strBuilder2);
                                    logger.info("AFTER DOING REVERSE OF THE GIVEN STRING IS:->"
                                                            + strBuilder1);
                                    if(String.valueOf(strBuilder1).compareTo(
                                                            String.valueOf(strBuilder2)) == 0)
                                                palindromeStatus = "GIVEN STRING IS A PALINDROME";
                                    else
                                                palindromeStatus = "GIVEN STRING IS NOT PALINDROME";
                        }
                        returnpalindromeStatus;
            }

            publicstatic void main(String[] args) {
                        logger.info("ENTER THE STRING FOR WHICH YOU WANT TO CHECK FOR PALINDROME");
                        Scanner scanner = new Scanner(System.in);
                        String strPalindrome = scanner.nextLine();

                        PalindromeTest pldTest = new PalindromeTest();
                        logger.info("PALINDROME TEST BY getPalindrome() METHOD:-"
                                                + pldTest.getPalindrome(strPalindrome.toLowerCase()));
                        logger.info("PALINDROME TEST BY checkPalindrome() METHOD:-"
                                                + pldTest.checkPalindrome(strPalindrome.toLowerCase()));
            }
}


RESULT:-

POSITIVE SCENARIO:-

Jun 15, 2013 9:07:47 PM com.gaurav.coreprograms.PalindromeTest main
INFO: ENTER THE STRING FOR WHICH YOU WANT TO CHECK FOR PALINDROME
RaceCar
Jun 15, 2013 9:07:52 PM com.gaurav.coreprograms.PalindromeTest getPalindrome
INFO: ACTULA GIVEN STRING IS:->racecar
Jun 15, 2013 9:07:52 PM com.gaurav.coreprograms.PalindromeTest getPalindrome
INFO: AFTER DOING REVERSE OF THE GIVEN STRING IS:->racecar
Jun 15, 2013 9:07:52 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY getPalindrome() METHOD:-GIVEN STRING IS A PALINDROME
Jun 15, 2013 9:07:52 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY findPalindrome() METHOD:-PALINDROME WORD
Jun 15, 2013 9:07:52 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY checkPalindrome() METHOD:-true

NEGATIVE SCENARIO:-

Jun 15, 2013 9:09:24 PM com.gaurav.coreprograms.PalindromeTest main
INFO: ENTER THE STRING FOR WHICH YOU WANT TO CHECK FOR PALINDROME
INDIA
Jun 15, 2013 9:09:32 PM com.gaurav.coreprograms.PalindromeTest getPalindrome
INFO: ACTULA GIVEN STRING IS:->india
Jun 15, 2013 9:09:32 PM com.gaurav.coreprograms.PalindromeTest getPalindrome
INFO: AFTER DOING REVERSE OF THE GIVEN STRING IS:->aidni
Jun 15, 2013 9:09:32 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY getPalindrome() METHOD:-GIVEN STRING IS NOT PALINDROME
Jun 15, 2013 9:09:32 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY findPalindrome() METHOD:-NOT A PALINDROME WORD
Jun 15, 2013 9:09:32 PM com.gaurav.coreprograms.PalindromeTest main
INFO: PALINDROME TEST BY checkPalindrome() METHOD:-false

ANAGRAM


An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. Any word or phrase that exactly reproduces the letters in another order is an anagram.

Two strings are known as anagram, when they contain same characters but in different order. For Example: - BALM and LAMBor GOD and DOG or CAFE and FACE etc. Anagrams are actually mix-up of characters in String.

A Java program to find the given word is an Anagramor not.

packagecom.gaurav.coreprograms;

importjava.util.Arrays;
importjava.util.Scanner;
importjava.util.logging.Logger;

/**
 * This program is responsible to check the two given Strings are anagrams or
 * not
 *
 * @author KUMAR GAURAV
 */
public classAnagramTest {

            privatefinal static Logger logger = Logger.getLogger(PalindromeTest.class
                                    .getName());

            /**
             * A way to find if the provided two Strings are anagram or not in Java. Two
             * arrays are equal if they contain the same elements in the same order.
             *
             * @return true
             */
            publicboolean findAnagram(String actualString, String anagramString) {

                        char[] charArrayForActualString = actualString.toCharArray();
                        char[] charArrayForAnagramString = anagramString.toCharArray();
                        Arrays.sort(charArrayForActualString);
                        Arrays.sort(charArrayForAnagramString);
                        booleancheckStatus = Arrays.equals(charArrayForActualString,
                                                charArrayForAnagramString);
                        returncheckStatus;
            }

            /**
             * Another way to find if the provided two Strings are anagram or not in
             * Java.
             *
             * @return true
             */
            publicboolean checkAnagram(String actualWord, String anagramWord) {

                        if(actualWord.length() != anagramWord.length()) {
                                    returnfalse;
                        }

                        for(int i = 0; i < anagramWord.length(); i++) {

                                    if(!actualWord.contains("" + anagramWord.charAt(i))) {
                                                returnfalse;
                                    }

                                    actualWord = actualWord
                                                            .replaceFirst("" + anagramWord.charAt(i), "");
                                    anagramWord = anagramWord.replaceFirst("" + anagramWord.charAt(i),
                                                            "");
                        }
                        returntrue;
            }

            publicstatic void main(String args[]) {
                        AnagramTest anagramTest = new AnagramTest();

                        logger.info("ENTER THE TWO STRING FOR WHICH YOU WANT TO CHECK FOR ANAGRAM WORDS");
                        Scanner scanner = new Scanner(System.in);
                        Scanner scanner1 = new Scanner(System.in);

                        String actualWord = scanner.nextLine();
                        String anagramWord = scanner1.nextLine();

                        if(actualWord != null && anagramWord != null) {
                                    if(actualWord.length() != 0 && anagramWord.length() != 0) {
                                                logger.info("USING FIRST WAY, IS THE PROVIDED WORDS ARE ANAGRAM BY USING isAnagram() METHOD :- "
                                                                        + anagramTest.findAnagram(actualWord, anagramWord));
                                                logger.info("USING SECOND WAY,IS THE PROVIDED WORDS ARE ANAGRAM BY USING checkAnagram() METHOD :- "
                                                                        + anagramTest.checkAnagram(actualWord, anagramWord));
                                    }
                        }

            }
}


RESULT:-

POSITIVE SCENARIO:-

Jun 15, 2013 9:05:46 PM com.gaurav.coreprograms.AnagramTest main
INFO: ENTER THE TWO STRING FOR WHICH YOU WANT TO CHECK FOR ANAGRAM WORDS
CAFE

FACE
Jun 15, 2013 9:05:50 PM com.gaurav.coreprograms.AnagramTest main
INFO: USING FIRST WAY, IS THE PROVIDED WORDS ARE ANAGRAM BY USING isAnagram() METHOD :- true
Jun 15, 2013 9:05:50 PM com.gaurav.coreprograms.AnagramTest main
INFO: USING SECOND WAY,IS THE PROVIDED WORDS ARE ANAGRAM BY USING checkAnagram() METHOD :- true

NEGATIVE SCENARIO:-

Jun 15, 2013 9:06:31 PM com.gaurav.coreprograms.AnagramTest main
INFO: ENTER THE TWO STRING FOR WHICH YOU WANT TO CHECK FOR ANAGRAM WORDS
TEAR

EAR
Jun 15, 2013 9:07:05 PM com.gaurav.coreprograms.AnagramTest main
INFO: USING FIRST WAY, IS THE PROVIDED WORDS ARE ANAGRAM BY USING isAnagram() METHOD :- false
Jun 15, 2013 9:07:05 PM com.gaurav.coreprograms.AnagramTest main
INFO: USING SECOND WAY,IS THE PROVIDED WORDS ARE ANAGRAM BY USING checkAnagram() METHOD :- false

No comments:

Post a Comment