简体   繁体   中英

How to check if a word is a palindrome based on the length of the word entered by user? no arrays please

I am wondering how to write a small java method which allows the user to enter a sentence and the program will return all of the palindromes in the sentence. BTW: a palindrome is a word that is the same front and backwards. First, I created a method to check if one word entered by the user is a palindrome. The code is shown below. Now, I must figure out when the user enters the length of what the word should be. For example: please enter a palindrome: [user enters: racecar] please enter the size of your word: [user enters 3] Here is your palindrome: cec [as you can see, the size of length of the new word is 3] I'm not particularly sure how to obtain the new palindrome. Could someone help me? Please do not use arrays!

Here is the small method I wrote which returns true if the word entered is a palindrome (this is not what my question is asking, I think that this is something I can build off of. Of course I would return a String, so I would make the return type 'void'.)

public boolean printPalindrome(String sentence, int size)
{
String reverseStr = "";
for(int i = (sentence.length()-1); i>=size; i--)
{
reverseStr += sentence.charAt(i);
}
if(sentence.toLowerCase().equals(reverseStr.toLowerCase()))
return true;
else
return false;
} 

This will return the first palindrome it gets and if the size is more the the actual sentece the size will change to the sentece max length and if no palindrome is found it will return an empty string

public String printPalindrome(String sentence, int size) {
    if (size < 0) return "";
    if (size > sentence.length()) size = sentence.length();
    for (int i = 0; i + size <= sentence.length(); i++) {
        String miniStr = sentence.substring(i, i + size);
        String reverseStr = new StringBuilder(miniStr).reverse().toString();
        if (reverseStr.equals(miniStr)) return miniStr;
    }
    return "";
}

From the example, I believe the O/P is seeking help in finding if a palindrome contains shorter palindromic sequences of a specified length. The O/P already demonstrated he / she knows how to code a basic palindrome test.

Here is an example of part of the code that solves the O/P's problem:

public static List<String> findSubPalindromes
        (String sentence, int subLength) {
            
    List<String> result = new ArrayList<> ();        
    
    for (int i = 0; i < sentence.length() - subLength; ++i) {
        if (isPalindrome (sentence.substring (i, i + subLength))) {
            result.add (sentence.substring (i, i + subLength));
        }
    }
    return result;
}
        
public static boolean isPalindrome (String str) {
    
    int len = str.length() - 1 ;
    for (int i = 0; i <= str.length () / 2; ++i) {
        if (Character.toLowerCase(str.charAt(i)) != 
            Character.toLowerCase(str.charAt(len - i)))
            return false;                       
    }
    return true;
}

The idea is to first have a boolean isPalindrome (String) method. That method is called in a loop, testing each substring of the specified length.

The code to prompt the user for a palindrome, test to see if the user entered a palindrome, prompt for a sub-length, test the validity of the sub-length would be in a third method, which is not included in this answer.

Perhaps off-topic:

A traditional palindrome tests only letters, ignoring not only case (as in the O/P code), but also ignores spaces and punctuation. Example: "A man, a plan, a canal: Panama," or "Madam. I'm Adam." This has not been addressed.

Is this small enough?

public List<String> findPalindromes(String sentence) {
    return Arrays.stream(sentence.split(" ")).filter(s -> new StringBuilder(s).reverse().toString().equals(s)).collect(Collectors.toList());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM