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

时间:2019-08-23 02:02:58

标签: java boolean palindrome

我在课堂上被要求确定一个句子字符串(忽略大写/小写,标点和空格)是否是回文,并相应地返回true / false。

我已经看过这里有关回文字符串的一些讨论,但是我似乎仍然无法弄清楚我的代码到底有什么问题,特别是因为它在某些情况下有效,而在另一些情况下则无效。

在课堂上我有prive字符串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}", "");
      }

例如,以“圣诞老人在NASA住过魔鬼”作为我的输入,我应该得到“ true”,但始终返回“ false”。

1 个答案:

答案 0 :(得分:0)

如果您使用new Sentence("A Santa lived as a devil at NASA").isPalindrome(),那么您会做lowerCase