(5) 判断字符串或数字是否是回文的

/**
 * 判断一个数字或字符串是不是回文的;
 *
 */
public class HuiWenShu {
	
	public static void main(String[] args) {
		System.out.println(HuiWenShu.isHuiWenShu(12321));
		System.out.println(HuiWenShu.isHuiWenShu(String.valueOf(12321)));
		System.out.println(HuiWenShu.isHuiWenShu(";abcba;"));
	}
	
	//判断字符串是不是回文的
	private static boolean isHuiWenShu(String str) {
		StringBuffer stringBuffer = new StringBuffer(str);
		stringBuffer.reverse();
		return stringBuffer.toString().equals(str);
	}
	
	//判断数字是不是回文的
	private static boolean isHuiWenShu(int num) {
		int oldValue = num;
		int temp = 0;
		//把数字倒置
		while (num > 0) {
			temp = temp * 10 + num % 10;
			num /= 10;
		}
		return temp == oldValue;
	}
}

原文地址:https://www.cnblogs.com/xiaozhang2014/p/5297285.html