This Java program provides a variety of features related to palindromes, allowing users to:
- Check if a word is a palindrome.
- Find palindromes in a paragraph.
- Check if a number is a palindrome.
- Generate palindromic numbers within a range.
- Find the longest palindromic substring in a string.
- Find all palindromic substrings in a string.
When you run the program, you will be presented with a menu of options. Simply select the number corresponding to the operation you wish to perform, and follow the prompts to provide input.
- Description: Prompts the user to enter a word and checks whether it reads the same backward as forward.
- Input Example:
radar
- Output Example:
Is the word a palindrome? true
- Description: Takes a paragraph and identifies all the palindromic words within it.
- Input Example:
Madam Arora teaches malayalam.
- Output Example:
Palindromes found in the paragraph: madam arora malayalam
- Description: Prompts the user to enter a number and determines if it is a palindrome.
- Input Example:
12321
- Output Example:
Is the number a palindrome? true
- Description: Asks for a range (start and end) and prints all palindromic numbers within that range.
- Input Example: Start:
10
, End:200
- Output Example:
Palindromic numbers within the range: 11 22 33 ... 191
- Description: Identifies the longest substring within a given string that is a palindrome.
- Input Example:
babad
- Output Example:
The longest palindromic substring is: bab
- Description: Finds and lists all palindromic substrings in the given input string.
- Input Example:
aaa
- Output Example:
Palindromic substrings found: a aa aaa
- Handles the menu and user input.
- Calls appropriate methods based on user choice.
CheckPalindrome(String word)
: Checks if a given string is a palindrome.cleanWord(String word)
: Cleans a string by removing non-alphanumeric characters and converting to lowercase.reverse(String s)
: Reverses the input string.findPalindromes(String paragraph)
: Extracts palindromic words from a paragraph.generatePalindromicNumbers(int start, int end)
: Prints all palindromic numbers within a range.longestPalindrome(String s)
: Finds the longest palindromic substring in a string.findAllPalindromicSubstrings(String s)
: Identifies all palindromic substrings in a string.expandAroundCenter(String s, int left, int right, Set<String> palindromes)
: Helper method to identify substrings that are palindromes.
- Compile the program:
javac Palindrome.java
- Run the program:
java Palindrome
- Follow the on-screen menu to interact with the program.
- Java Development Kit (JDK) installed on your system.
- Basic knowledge of running Java programs from the command line.
- This program ignores case and special characters when checking for palindromes.
- It efficiently identifies palindromic substrings using the center expansion technique.
Feel free to explore the code and adapt it to your needs!