Cracking The Coding Interview 1.2

时间:2021-09-02 16:50:24
//原文:
//
// Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
//
// 从前向后交换,到中间为止 #include <iostream>
using namespace std;
void mSwap(char &a, char &b)
{
char c=a;
a=b;
b=c;
}
void mReverse(char *str)
{
if (str == NULL)
{
return;
}
int size = strlen(str);
for (int i =0;i< size/2; i++)
{
mSwap(str[i], str[size-1-i]);
}
}
int main()
{
char s[] = "abcdefg";
cout << s <<endl;
mReverse(s);
cout << s <<endl;
return 0;
}