[英]Clean string method for a recusrion palindrome java program?
This is a recursion program to test whether or not a sentence is a palindrome. 这是一个递归程序,用于测试句子是否是回文。 It will run correctly if I write "bob" but not for "Madam I'm Adam" because of the caps and symbols. 如果我写“bob”,它将正确运行,但因为大写字母和符号而不是“Madam I'm Adam”。 We are required to use a clean string method(?) to eliminate the spaces, symbols, and caps. 我们需要使用干净的字符串方法(?)来消除空格,符号和大写字母。 This is what I have but I don't believe I've implemented it correctly. 这就是我所拥有的,但我不相信我已经正确实现了它。 Could someone tell me how to improve/fix this? 有人能告诉我如何改进/解决这个问题吗? (Yes, I've looked 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);
}
}
Your recursive method isaPalindrome
is correct. 您的递归方法isaPalindrome
是正确的。 If you want to further improve it, I would suggest you to avoid using subString to create parameters for your recursive call , this will create too many strings. 如果你想进一步改进它,我建议你避免使用subString为你的递归调用创建参数 ,这将创建太多的字符串。
Instead, keep track of the positions of the characters in the original string that you are comparing : 而是跟踪您要比较的原始字符串中字符的位置 :
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;
}
You would invoke the method as: isaPalindrome(inStr, 0, inStr.length() - 1)
你可以调用方法: isaPalindrome(inStr, 0, inStr.length() - 1)
As for your clean method, you can use toLowerCase
and Character.isLetter
method to process the original string. 至于clean方法,可以使用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();
}
Try this: 尝试这个:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.