判断一个数是否为对称数或对称字符串

判断一个数是否为对称数,如:123321,2332,abccba等等。

分析:1.字符串长度必须大于1;

         2.字符串长度必须为偶数;

思路:将字符串截成两个等长字符串分别放到两个字符数组里,然后进行字符比较,得出结果

public boolean testIn(String string)
{
     // TODO Auto-generated method stub
     int lenght = string.length();
     if (lenght % 2 == 0 && lenght != 0)
     {
          char[] ch1 = string.substring(0, lenght / 2).toCharArray();
          char[] ch2 = string.substring(lenght / 2, lenght).toCharArray();
          int i = 0;
          int j = lenght / 2 - 1;
          while(ch1[i] == ch2[j])
          {
               if (j == 0)
               {
                    return true;
               }
               i++;
               j--;
          }
     }
     return false;
}

 

你可能感兴趣的:(J#)