Knowee
Questions
Features
Study Tools

Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word  (or) no palindromeThe nextline contains the biggest palindrome word

Question

Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word  (or) no palindromeThe nextline contains the biggest palindrome word

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java program that will find the smallest and biggest possible palindrome word in a given string. If no palindrome is found, it will print 'no palindrome'.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] words = str.split(" ");
        String minPalin = null;
        String maxPalin = null;

        for (String word : words) {
            if (isPalindrome(word)) {
                if (minPalin == null || word.length() < minPalin.length()) {
                    minPalin = word;
                }
                if (maxPalin == null || word.length() > maxPalin.length()) {
                    maxPalin = word;
                }
            }
        }

        if (minPalin == null) {
            System.out.println("no palindrome");
        } else {
            System.out.println("Smallest palindrome: " + minPalin);
            System.out.println("Biggest palindrome: " + maxPalin);
        }
    }

    public static boolean isPalindrome(String str) {
        int i = 0, j = str.length() - 1;
        while (i < j) {
            if (str.charAt(i) != str.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

This program works by splitting the input string into words, then checking each word to see if it's a palindrome. If it is, it checks if it's smaller or larger than the current smallest or largest palindrome found so far. If it is, it updates the smallest or largest palindrome. At the end, it prints out the smallest and largest palindromes found, or 'no palindrome' if none were found.

This problem has been solved

Similar Questions

Your friend is in university and there is a coding challenge going on. A string S of lowercase letters is displayed over the screen. The length of the string is N. An integer M is also displayed over the screen.Your task is write a program to determine if we can make the string Palindrome by performing at most M operations on the provided string

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.InputThe first line contains integer t, the number of test cases. Followed by t lines containing integers K.OutputFor each K, output the smallest palindrome larger than K.ExampleInput:28082133Output:8182222Warning: large Input/Output data, be careful with certain languages

In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes.Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.Cases not allowed –After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.No character may be shared in forming 3 palindromes.Constraints:Constraints1 <= the length of input sting <= 1000InputFirst line contains the input string consisting of characters between [a-z].OutputPrint 3 substrings one on each line.Example:Example 1InputnayannamantenetOutputnayannamantenetExplanation:The original string can be split into 3 palindromes as mentioned in the output.However, if the input was nayanamantenet, then the answer would be “Impossible”.

Write a Java program to find longest Palindromic Substring within a string CopySample Output:The given string is: thequickbrownfoxxofnworbquicktheThe longest palindrome substring in the given string is; brownfoxxofnworbThe length of the palindromic substring is: 16

Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.