今天参加了恒生电子的实习生笔试,鸡蛋,数据库的完全不会,看来要看的东西还有很多啊。
笔试最后一题是写一个算法来判断回文字符串,比如“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;
}