好的,所以我创建了一个程序来定义插入的单词是否是回文。
但是我需要帮助删除要在字符串中插入数字的地方。

import java.util.*;
import java.util.Scanner;
class Palindrome
{
   public static void main(String args[])
   {
      String reverse = "";
      Scanner scan = new Scanner(System.in);
      System.out.print("Type a sentence and press enter: ");
      String input = scan.nextLine();
      // use regex to remove the punctuation and spaces
      String Input = input.replaceAll("\\W", " ");
      System.out.println(Input);

      int length = input.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse.replaceAll("\\W", "") + input.charAt(i);
      System.out.println(reverse);

      if (input.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
        System.out.println("Entered string is not a palindrome.");
   }
}

最佳答案

如果要删除数字,请尝试input.replaceAll("[0-9]","")

07-25 21:29
查看更多