[笔试]判断回文数

今天参加了恒生电子的实习生笔试,鸡蛋,数据库的完全不会,看来要看的东西还有很多啊。

笔试最后一题是写一个算法来判断回文字符串,比如“ABCDCBA”。

后来重新在机子上跑了下,唉。。。果然考试的时候貌似把指针写错了,而且还写的太复杂了,指针这东西真心略烦啊。



C++代码

/*
 *作者:RogerKing
 *邮箱: jin_tengfei@163.com
 *日期:2014/5/11 星期日
 */

#include <iostream>
#include <string>

using namespace std;

void IsPalindrome(char  *s)
{
	char *beg=s;
	char *end=beg+strlen(s)-1;

	while(beg<end)
	{
		if( *beg++ != *end--)
			cout <<s<<"不是回文"<<endl;
	}
	cout <<s<<"是回文"<<endl;

}
int main()
{
	char A[100];
	cout<<"输入字符串:"<<endl;
	cin>>A;
	IsPalindrome(A);
	return 0;
}

还可以这样写

#include <iostream>
#include <string>

using namespace std;

void IsPalindrome(char *str)
{
	int start = 0, end;
	end = strlen(str) - 1;
	while(end - start >= 1)
	{
		if(str[start] != str[end])
			cout <<str<<"不是回文"<<endl;;
		++start;
		--end;
	}
	cout <<str<<"是回文"<<endl;
}
int main()
{
	char A[100];
	cout<<"输入字符串:"<<endl;
	cin>>A;
	IsPalindrome(A);
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值