Clear string method for recusrion pavilion pastungia program?

This is a recursion program to check if a sentence is a palindrome. It will work correctly if I write "bob", but not for "Madam i am Adam" due to caps and characters. We should use the clean line method (?) To remove spaces, characters, and caps. This is what I have, but I do not believe that I implemented it correctly. Can someone tell me how to improve / fix this? (Yes, I watched all over the Internet)

import java.util.Scanner;

public class Palindromes {

public static boolean isaPalindrome(String s) {

    String cleanedString = clean(s);
    if (s.length() == 0 || s.length() == 1)
        return true;
    if (s.charAt(0) == s.charAt(s.length() - 1))
        return isaPalindrome(s.substring(1, s.length() - 1));
    return false;
}

public static void main(String[] args) {
    System.out.print("Enter a palindrome to test: ");

    Scanner console = new Scanner(System.in);

    String inStr = console.nextLine();

    if (isaPalindrome(inStr)) {

        System.out.printf("The input string, %s, is a palindrome.\n",
                inStr);

        reverseStr(inStr); // must be recursive!

        System.out.println();
    } else {
        System.out.printf("The input string, %s, is not a palindrome.\n",
                inStr);
    }
}

private static String clean(String s) {

    String cleaned = "";
    return cleaned;
}

private static String reverseStr(String inStr) {
    if ((null == inStr) || (inStr.length() <= 1)) {
        return inStr;
    }
    return reverseStr(inStr.substring(1)) + inStr.charAt(0);
}
}
+4
source share
2 answers

isaPalindrome . , subString , .

, :

public static boolean isaPalindrome(String s, int leftIndex, int rightIndex) {
    if (leftIndex == rightIndex) return true;

    if (s.charAt(leftIndex) == s.charAt(rightIndex))
        return isaPalindrome(s, leftIndex + 1, rightIndex - 1);
    return false;
}

: isaPalindrome(inStr, 0, inStr.length() - 1)

, toLowerCase Character.isLetter .

private static String clean(String s) {
    String lowerCaseString = s.toLowerCase();

    StringBuffer result = new StringBuffer();

    for (int i = 0; i < lowerCaseString.length(); ++i) {
        if (Character.isLetter(lowerCaseString.charAt(i))) {
            result.append(lowerCaseString.charAt(i));
        }
    }

    return result.toString();
}
+2

:

public static void main(final String[] args) {
        final String unclean = "Madam I'm Adam";
        final String clean = cleanString(unclean);
        System.out.println("Clean string is: " + clean);
    }

    static private String cleanString(final String pTheString) {
        final StringBuilder sb = new StringBuilder(pTheString.length());
        for (final char c : pTheString.toCharArray()) {
            switch (c) {
                // ignore all those
                case ' ':
                case '\'':
                case '.':
                    break;

                    // write the rest
                default:
                    sb.append(c);
            }
        }
        return sb.toString().toLowerCase();
    }
0

Source: https://habr.com/ru/post/1616826/


All Articles