^&^
1
#include<iostream>2

3
using namespace std;4

5
bool huiwen(char* str)6


{7
char* end=str;8
char* first=str;9
int temp=strlen(str)/2+1;10
while(*end++!='\0')11
NULL;12
end--; //point to the char '\0'13
while(temp-->0)14

{15
if(*first++!=*(--end)) //--end in order to point to the char before '\0'16
return false;17
}18
return true;19
}20

21
bool huiwen2(char* str)22


{23
int length=strlen(str);24
for(int i=0; i<length; i++)25
if(*(str+i)!=*(str+length-i-1))26
return false;27
return true;28
}29
int main()30


{31
char* str="hongshenggnehsgnoh";32
cout<<huiwen(str)<<endl;33
cout<<huiwen2(str)<<endl;34
return 0;35
}36

本文介绍了两种判断字符串是否为回文的方法:一种使用指针从两端向中间比较字符;另一种利用循环,通过索引对比首尾字符的方式进行验证。代码示例清晰地展示了这两种方法的具体实现。
5038

被折叠的 条评论
为什么被折叠?



