检查句子字符串是否为回文

Checking if a Sentence String is a Palindrome

我在 class 中被要求确定一个句子字符串(忽略 upper/lower 大小写、标点符号和空格)是否是回文,并且 return true/false 相应地。

我已经看过这里关于回文字符串的几个讨论,但我似乎仍然无法弄清楚我的代码到底出了什么问题,尤其是因为它在某些情况下有效但在某些情况下无效其他。

在 class 句子中我有 prive String mySentence 和 private int myNumWords。关于静态布尔值 isPalindrome,我被鼓励尝试递归,但我想先尝试迭代。


      //Constructor.  Creates sentence from String str.
      //                        Finds the number of words in sentence.
      //Precondition:  Words in str separated by exactly one blank.
       public Sentence( String str )
      { 
         mySentence = str; 
         int count = 0;
         for(int k = 0; k<str.length(); k++)
         {
            if(str.substring(k,k+1).equals(" "))
               count++;
         }
         myNumWords = count + 1;
      }

       public int getNumWords()
      {  
         return myNumWords;  
      }

       public String getSentence()
      {
         return mySentence; 
      }
      //Returns copy of String s with all blanks removed.
      //Postcondition:  Returned string contains just one word.
      private static String removeBlanks( String s )
      {  
          return s.replaceAll(" ", ""); 
      }
      //Returns true if mySentence is a palindrome, false otherwise.
       public boolean isPalindrome()
      {
         boolean palindrome = true;
         String sentence = removeBlanks(mySentence);
         String newsentence = removePunctuation(sentence);

         int indice1 = 0;
         int indice2 = newsentence.length() - 1; 
         while(indice1 < indice2)
         {
            if(newsentence.charAt(indice1)!= newsentence.charAt(indice2))
               palindrome = false;
            indice1++;
            indice2--;
         }
         return palindrome;          
      }
      //Precondition: s has no blanks, no punctuation, and is in lower case.
      //Returns true if s is a palindrome, false otherwise.
      private static boolean isPalindrome( String s, int start, int end )
      {
         boolean palindrome = true;
         String sentence = removeBlanks(s);
         String newsentence = removePunctuation(sentence); 

         int indice1 = 0;
         int indice2 = newsentence.length() - 1; 
         while(indice1 < indice2)
         {
            if(newsentence.charAt(indice1)!= newsentence.charAt(indice2))
               palindrome = false;
            indice1++;
            indice2--;
         }         
         return palindrome;            
      }      
      //Returns copy of String s with all letters in lowercase.
      //Postcondition:  Number of words in returned string equals
      //                        number of words in s.
       private static String lowerCase( String s )
      {  
         return s.toLowerCase(); 
      }

      //Returns copy of String s with all punctuation removed.
      //Postcondition:  Number of words in returned string equals
      //                        number of words in s.
       private static String removePunctuation( String s )
      { 
         return s.replaceAll("\p{Punct}", "");
      }

例如,对于 "A Santa lived as a devil at NASA" 作为我的输入,我应该得到 "true",但保持 returning "false".

如果你使用 new Sentence("A Santa lived as a devil at NASA").isPalindrome() 那么你忘记做 lowerCase