在给定字符串中打印最小和最大可能回文单词的Java程序
回文字符串是一个字符串,其反转字符串等于原始字符串。在这个Java程序中,我们将看到打印字符串中最小和最大回文单词的方法。
给定一个字符串形式的单词句子,我们需要打印这些单词中最小和最长的回文单词。
例子:
Input: Wow madam is driving racecar.
Smallest Palindrome: Wow
Longest Palindrome: racecar
Explanation: The string contains three palindrome words (i.e., madam, Wow, racecar)
but the length of racecar is greatest and that of wow is smallest.
Input: "Nitin is a good guy"
Smallest Palindrome: a
Longest Palindrome: Nitin
方法:
- lengthPalindrome()函数通过提取字符串的每个单词并将其传递给 checkPalin()函数来找到最长和最小的回文单词,然后通过将其与原始字符串的长度进行比较来更新最小和最长字符串的答案字符串,如果它是回文。
- checkPalin()函数检查单词是否为回文。如果单词是回文则返回真,否则返回假。它确保空字符串不被算作回文,因为用户可以在字符串之间或开头输入多个空格
例子:
Java
// Java program to print the smallest and
// longest palindromic word out of the words of a sentence
public class Main {
// Function to check if a
// word is palindrome
public static boolean checkPalin(String word)
{
int n = word.length();
// making the check case
// case insensitive
word = word.toLowerCase();
// loop to check palindrome
for (int i = 0; i < n; i++, n--)
{
if (word.charAt(i) != word.charAt(n - 1))
return false;
}
return true;
}
// Determine the smallest and biggest
// palindromes in a given string
public static void lengthPalindrome(int temp, String words[])
{
int count = 0;
String smallest = "", longest = "";
for (int i = 0; i < temp; i++) {
if (checkPalin(words[i])) {
count++;
// Initialize smallest and longest
// when first palindromic word
// is found
if (count == 1)
smallest = longest = words[i];
// Compare smallest and longest with each
// palindromic words
else {
// If length of smallest is greater
// than next palindromic word then
// Store that word in smallest
if (smallest.length()
> words[i].length())
smallest = words[i];
// If length of longest is less
// than next palindromic word then
// Store that word in longest
if (longest.length()
< words[i].length())
longest = words[i];
}
}
}
if (count == 0)
System.out.println("No palindrome found");
else {
System.out.println("Smallest palindrome: "
+ smallest);
System.out.println("Biggest palindrome: "
+ longest);
}
}
public static void main(String[] args)
{
String string = "Wow Madam is driving racecar";
String word = "";
String[] words = new String[100];
int temp = 0;
// Add extra space after string
// to get the last word
string = string + " ";
for (int i = 0; i < string.length(); i++) {
// Split the string into words
if (string.charAt(i) != ' ') {
word = word + string.charAt(i);
}
else {
// Add word to array words
words[temp] = word;
temp++;
word = "";
}
}
System.out.println("Inputted String : " + string);
lengthPalindrome(temp, words);
}
}
输出
Inputted String : Wow Madam is driving racecar
Smallest palindrome: Wow
Biggest palindrome: racecar