#include <iostream>
#include <cstring>
using namespace std;
void swap(char &a, char &b)
{
a = a^b;
b = a^b;
a = a^b;
}
void reverse2(char *s)
{
int n = strlen(s);
for(int i=0; i<n/2; ++i)
swap(s[i], s[n-i-1]);
}
void reverse(char *s)
{
char *end = s;
char tmp;
if(s)
{
while(*end)
++end;
--end;
while(s < end)
{
tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}
}
int main()
{
char s[] = "1234567890";
reverse2(s);
cout<<s<<endl;
return 0;
}两种字符串逆序方式
最新推荐文章于 2022-08-05 17:33:52 发布
本文介绍了一种使用位运算实现字符串反转的方法,并提供了两种不同的实现方案。一种是利用了一个swap函数来交换字符,另一种则直接在reverse函数内部完成字符的交换。这两种方法都有效地实现了字符串的反转。
253

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



