标签:change null star tar hello auto reverse 反转字符串 style
1.定义反转字符串
void changeOrder1(char* data)
{
if (data == NULL)
{
return;
}
auto nLen = strlen(data);
char *p1 = data;
char* p2 = p1 + (nLen - 1);
while (p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
}
void changeOrder2(char* data)
{
auto nLen = strlen(data);
if (nLen <= 1)
{
return;
}
auto nMid = nLen / 2;
auto nStart = 0;
while (nStart < nMid)
{
std::swap(data[nStart], data[nLen - nStart - 1]);
++nStart;
}
}
void changeOrder3(char* data)
{
auto nLen = strlen(data);
std::reverse(data, data + nLen);
}
2.测试如下:
char s[] = "hello world"
changeOrder3(s);
标签:change null star tar hello auto reverse 反转字符串 style
原文地址:https://www.cnblogs.com/kinglxg/p/13170259.html