/**
* @(#)palindrome1.java
*
* palindrome1 application
*
* @author
* @version 1.00 2013/11/15
*/
public class palindrome1 {
static boolean isPalindrome(String str) {
int count=0;
//check all characters of sequence is palindrome
for(int i = 0; i < str.length();i++)
{
if(str.charAt(i)!=str.charAt(str.length()-1-i))
{
return false;
}
}
//if it is return true otherwise return false
return true;
}
public static void main(String[] args) {
// TODO, add your application code
String sentence= "bob gave that pop race car to me." ;
String sentenceMax="";
String sentenceNew="";
sentence = sentence.replace( " ","");
for(int i = 0;i<sentence.length();i++)
{
for(int k=0;k<i;k++)
{
sentenceNew = sentence.substring(k,i);
if(isPalindrome(sentenceNew)&&sentenceNew.length()>sentenceMax.length());
{
sentenceMax=sentenceNew;
sentenceNew="";
}
}
}
System.out.println(sentenceMax);
}
}
問題は、ユーザーに文を尋ね、文中のスペースを無視して文中の最長の回文部分文字列を見つけ、最長の回文部分文字列を出力することです。パート B で記述した関数を使用する必要があります。文は大文字と小文字を区別しない必要があります。パート B は、mycode の isPalindrome() という名前の最初のメソッドです。出力は次のようになります。
racecar
しかし、私のコードは次のように出力します:
e
私のコードで何が間違っていますか?