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.
package
com.gaurav.coreprograms;
import
java.util.Scanner;
import
java.util.logging.Logger;
/**
* This program is responsible to find the
given String is a palindrome or not.
*
* @author KUMAR GAURAV
*
*/
public class
PalindromeTest {
private
final static Logger logger = Logger.getLogger(PalindromeTest.class
.getName());
/**
* This is a way to find the given word is a palindrome.
* */
public
boolean checkPalindrome(String inputWord) {
try
{
char[]
charArray = inputWord.toCharArray();
int
arrayLength = charArray.length - 1;
int
middleOfArray = Math.round(charArray.length / 2);
for
(int i = arrayLength; i >= middleOfArray; i--) {
char
leftPosition = charArray[arrayLength - i];
char
rightPosition = charArray[i];
if
(leftPosition != rightPosition)
return
false;
}
}
catch (Exception e) {
logger.severe("ERROR:-"
+ e.getMessage());
}
return
true;
}
/**
* This is another way to find the given word
is a palindrome.
* */
public
String 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";
}
return
palindromeStatus;
}
public
static 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 LAMB
or 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 Anagram
or not.
package
com.gaurav.coreprograms;
import
java.util.Arrays;
import
java.util.Scanner;
import
java.util.logging.Logger;
/**
* This program is responsible to check the two
given Strings are anagrams or
* not
*
* @author KUMAR GAURAV
*/
public class
AnagramTest {
private
final 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
*/
public
boolean findAnagram(String actualString, String anagramString) {
char[]
charArrayForActualString = actualString.toCharArray();
char[]
charArrayForAnagramString = anagramString.toCharArray();
Arrays.sort(charArrayForActualString);
Arrays.sort(charArrayForAnagramString);
boolean
checkStatus = Arrays.equals(charArrayForActualString,
charArrayForAnagramString);
return
checkStatus;
}
/**
* Another way to find if the provided two
Strings are anagram or not in
* Java.
*
* @return true
*/
public
boolean checkAnagram(String actualWord, String anagramWord) {
if
(actualWord.length() != anagramWord.length()) {
return
false;
}
for
(int i = 0; i < anagramWord.length(); i++) {
if
(!actualWord.contains("" + anagramWord.charAt(i))) {
return
false;
}
actualWord
= actualWord
.replaceFirst(""
+ anagramWord.charAt(i), "");
anagramWord
= anagramWord.replaceFirst("" + anagramWord.charAt(i),
"");
}
return
true;
}
public
static 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