回文数(字符串操作)

题目:

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

示例 1:
输入: 121
输出: true

思路:

把数字先转换为字符串theX,然后倒置字符串theX2,判断theX与theX2是否相等,若相等则是回文数。

代码:


class Solution {
public:
    bool isPalindrome(int x) {
    	char theX[10];
    	itoa(x,theX,10);//#include:将数字转换为字符数组
    	char theX2[10];
    	strcpy(theX2,theX);//#include:将theX复制为theX2
    	strrev(theX);//#include :将字符串倒序
    	int result=strcmp(theX,theX2);//#include:比较两个字符串,相等则返回0
    	if(result==0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }

};

你可能感兴趣的:(回文数(字符串操作))