判断字符串是不是回文,使用C++、Python两种语言

// 这里的题目使用  “C++和Python"  两种语言解决</span>
//题目, 判断一个字符串是不是“回文”


#include <iostream>
using namespace std;

//! core
bool is_palindrome(char * s){
	int end = strlen(s) - 1;
	int pre = 0;
	while(pre < end){
		if(s[pre] != s[end])
			return false;
		pre ++ ;
		end -- ;
	}
	return true;
}


int main(){
	char s[] = "abccba";
	bool test, test2;
	test = false;
	test2 = false;

	test = is_palindrome(s);
	cout << test << endl;  // 输出1,则是回文; 输出0, 就不是回文

	char s2[] = "12";
	
	test2 = is_palindrome(s2);
	cout << test2 << endl;

	return 0;
}


Python版本答案:

#encoding=utf-8

#! core
def is_palindrome(s):
    end = len(s) - 1
    i = 0

    while(s[i] != s[end]):
        if s[i] != s[end]:
            return False
        i += 1
        end -= 1

    return True


def main():
    b_test1 = False
    b_test2 = False

    s1 = "abccba"
    s2 = "12"

    b_test1 = is_palindrome(s1)
    # 输出True,则是回文; 输出False, 就不是回文
    print b_test1

    b_test2 = is_palindrome(s2)
    print b_test2

main()



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值