Java  1.0
Static Public Member Functions | List of all members
palindromes Class Reference

Class for checking whether strings are palindromes or anagrams. More...

Static Public Member Functions

static boolean isPalindrome (String s)
 Checks whether a word is a palindrome. More...
 
static String reverse (String s)
 Reverses a given String. More...
 
static String revString (String str)
 Recursive version.
More...
 
static String reverseWords (String s)
 Reverses each word in a phrase. More...
 
static String deAccent (String str)
 Removes diacritical marks, aka accents. More...
 
static String removeSpacesAndPonctuation (String s)
 Removes spaces and punctuation from a String. More...
 
static boolean areAnagrams (String s1, String s2)
 Checks whether two strings form an anagram. More...
 
static boolean isAnagram (String a, String b)
 
static boolean areMutualPalindromes (String s1, String s2)
 Checks whether two strings are mutual palindromes. More...
 
static boolean isPhrasePalindrome (String s)
 Checks whether a phrase is a palindrome. More...
 
static void printPalindromeNumbers (int start, int amount)
 Prints the first palindrome numbers beginning at a given point. More...
 
static void main (String[] args)
 main function for testing. More...
 

Detailed Description

Class for checking whether strings are palindromes or anagrams.


Examples:

  1. civic, deified, dewed, kayak, level, madam, minim,
  2. racecar, radar, redder, refer, rotator, rotavator, rotor,
  3. sagas, solos, sexes, stats.

Examples (mutual palindromes):

  1. stressed / desserts
  2. samaroid (resembling a samara) / dioramas
  3. rewarder / redrawer
  4. animal / lamina
  5. departer / retraped (verb trape is recorded as an alternative spelling of traipse)
  6. reporter / retroper

Examples (palindrome phrases):

  1. "Anotaram a maratona"
  2. "Sairam o tio Sa e as oito Marias"
  3. "Roma me tem amor"
  4. "Socorram-me, subi no onibus em marrocos"

An anagram is a word or phrase formed by reordering the letters of another word or phrase, such as "satin" to "stain".

Examples:

  1. "porta" and "tropa"
  2. "Mary" and "Army".
  3. "Tom Cruise" and "So I'm cuter"
  4. "Tom Marvolo Riddle" and "I am Lord Voldemort"
  5. "Mother-in-law" and "Woman Hitler"
  6. "The earthquakes" and "That queer shake"
  7. "Debit card" and "Bad credit"
  8. "Slot machines" and "Cash lost in 'em"
  9. "School master" and "The classroom"
  10. "Eleven plus two" and "Twelve plus one"
  11. "Dormitory" and "Dirty room"
  12. "Punishment" and "Nine Thumps"
  13. "Desperation" and "A rope ends it"
  14. "The Morse code" and "Here come dots"
  15. "Snooze alarms" and "Alas! No more Zs"
  16. "A decimal point" and "I'm a dot in place"
  17. "Astronomer" and "Moon starer"
  18. "Fir cones" and "Conifers"
  19. "The eyes" and "They see"
  20. "Payment received" and "Every cent paid me"
  21. "Conversation" and "Voices rant on"
  22. "The public art galleries" and "Large picture halls, I bet"
  23. "Election results" and "Lies – let's recount"
  24. "Halley's Comet" and "Shall yet come"
  25. "The Hurricanes" and "These churn air"

To compile:

To run:

Java can be downloaded from: www.oracle.com

In linux, the version to be used can be set by doing: sudo alternatives –config java

Author
Paulo Roma
Since
29/12/2012
See also
http://www.fun-with-words.com/anag_example.html

Definition at line 72 of file palindromes.java.

Member Function Documentation

◆ areAnagrams()

static boolean palindromes.areAnagrams ( String  s1,
String  s2 
)
static

Checks whether two strings form an anagram.

An anagram is a type of word play, which is 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.

For example: "orchestra" can be rearranged into "carthorse".

Parameters
s1first string
s2second string.
Returns
true if s1 and s2 are anagrams, and false otherwise.

Definition at line 176 of file palindromes.java.

References deAccent(), and removeSpacesAndPonctuation().

Referenced by main().

◆ areMutualPalindromes()

static boolean palindromes.areMutualPalindromes ( String  s1,
String  s2 
)
static

Checks whether two strings are mutual palindromes.

Two words are mutual palindromes when they are the same when one of them is spelt in reverse.

Parameters
s1first string.
s2second string.
Returns
true if s1 and s2 are mutual palindromes, and false otherwise.
See also
Semordnilap

Definition at line 226 of file palindromes.java.

Referenced by main().

◆ deAccent()

static String palindromes.deAccent ( String  str)
static

Removes diacritical marks, aka accents.

It basically converts all accented characters into their deAccented counterparts, followed by their combining diacritics. Then, a regex can be used to strip off the diacritics.

Parameters
stra string.
Returns
string without accents.

Definition at line 141 of file palindromes.java.

Referenced by areAnagrams(), isPhrasePalindrome(), and main().

◆ isAnagram()

static boolean palindromes.isAnagram ( String  a,
String  b 
)
static

Definition at line 188 of file palindromes.java.

Referenced by main().

◆ isPalindrome()

static boolean palindromes.isPalindrome ( String  s)
static

Checks whether a word is a palindrome.

A palindrome is a word or phrase which is the same when spelt in reverse.

Parameters
sstring to be tested.
Returns
true if s is a palindrome, or false otherwise.
See also
icaltefl
treeleaves

Definition at line 84 of file palindromes.java.

Referenced by main(), and printPalindromeNumbers().

◆ isPhrasePalindrome()

static boolean palindromes.isPhrasePalindrome ( String  s)
static

Checks whether a phrase is a palindrome.

The only difficulty here is to remove spaces, accents and punctuation from the String.

For example: "A CARA RAJADA DA JARARACA."

Parameters
sstring to be tested.
Returns
true if s is a palindrome phrase, and false otherwise.

Definition at line 241 of file palindromes.java.

References deAccent(), removeSpacesAndPonctuation(), and reverse().

Referenced by main().

◆ main()

static void palindromes.main ( String[]  args)
static

main function for testing.

Parameters
argsstring to be tested for being a palindrome word, and a string to be tested for being a palindrome phrase.

Definition at line 266 of file palindromes.java.

References areAnagrams(), areMutualPalindromes(), deAccent(), isAnagram(), isPalindrome(), isPhrasePalindrome(), printPalindromeNumbers(), reverse(), reverseWords(), and revString().

◆ printPalindromeNumbers()

static void palindromes.printPalindromeNumbers ( int  start,
int  amount 
)
static

Prints the first palindrome numbers beginning at a given point.

Parameters
startbeginning point.
amountnumber of palindrome numbers to be printed.

Definition at line 252 of file palindromes.java.

References isPalindrome(), and baseConverter.itoa().

Referenced by main().

◆ removeSpacesAndPonctuation()

static String palindromes.removeSpacesAndPonctuation ( String  s)
static

Removes spaces and punctuation from a String.

Parameters
sstring to be processed.
Returns
an upper case string without spaces and punctuation.

Definition at line 153 of file palindromes.java.

Referenced by areAnagrams(), and isPhrasePalindrome().

◆ reverse()

static String palindromes.reverse ( String  s)
static

Reverses a given String.

Parameters
sstring to be reverse.
Returns
reversed string.

Definition at line 100 of file palindromes.java.

Referenced by isPhrasePalindrome(), main(), and reverseWords().

◆ reverseWords()

static String palindromes.reverseWords ( String  s)
static

Reverses each word in a phrase.

Parameters
sstring to be reversed.
Returns
reversed string.

Definition at line 123 of file palindromes.java.

References reverse().

Referenced by main().

◆ revString()

static String palindromes.revString ( String  str)
static

Recursive version.

Definition at line 111 of file palindromes.java.

Referenced by main().


The documentation for this class was generated from the following file: