您的位置:程序门 -> java -> j2se / 基础类



[讨论]判断是否回文,忽略标点和空格部分怎么写?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


[讨论]判断是否回文,忽略标点和空格部分怎么写?
发表于:2007-02-08 12:53:17 楼主
public   class   palindrome
{
        static   private   string   text;
        static   private   int   firstindex;
        static   private   int   lastindex;

  public   static   boolean   ispalindrome(string   text,   int   firstindex,int   lastindex)
        {
                text   =   text.tolowercase();
                firstindex   =   0;
                lastindex   =   text.length()-1;
             
for(   int   i=0;   i   <   text.length();   i++)
{
    char   letter   =   text.charat(i);
      if(character.isletterordigit(letter)
     
这里   我要忽略标点和空格部分,但是怎么继续写下去呢??

}
               
if   (firstindex   > =   lastindex)

                        return   true;     //   base   case


                else   if   (text.charat(firstindex)   !=   text.charat(lastindex))

                        return   false;
                else

                {
        return   ispalindrome(text,   firstindex   +   1,   lastindex   -1);
                }

               

        }

}  
发表于:2007-02-08 14:40:191楼 得分:0
有个思路。给你个思路。。
你可以把你字符串中的   标点和空格都换掉。换成“”
第二中方法你可拆分字符串。拆分点就是有标点和空格的地方。然后去掉然后在链接组成新的字符串。
发表于:2007-02-08 14:41:392楼 得分:0
该回复于2007-12-28 11:29:00被管理员或版主删除
发表于:2007-02-08 14:46:383楼 得分:0
扫描一下字符串嘛,不是标点或空格就填到新字符串中去,是的话就不要填


快速检索

最新资讯
热门点击