#include <iostream>
void swap(char& a, char& b)
{
char c = a;
a = b;
b = c;
}
// assumes both a and b point to enough memory to hold the
// longest of the strings.
void swap_strings(char* a, char* b)
{
unsigned i = 0, j = 0;
unsigned eos_count = 0; // end-of-string count.
while (eos_count < 2)
{
swap(a[i], b[j]);
if (!a[i])
++eos_count;
if (!b[j])
++eos_count;
++i, ++j;
}
}
int main()
{
char one[32] = "one";
char three[32] = "three";
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
swap_strings(one, three);
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdm9pZCBzd2FwKGNoYXImIGEsIGNoYXImIGIpCnsKICAgIGNoYXIgYyA9IGE7CiAgICBhID0gYjsKICAgIGIgPSBjOwp9CgovLyBhc3N1bWVzIGJvdGggYSBhbmQgYiBwb2ludCB0byBlbm91Z2ggbWVtb3J5IHRvIGhvbGQgdGhlIAovLyBsb25nZXN0IG9mIHRoZSBzdHJpbmdzLgp2b2lkIHN3YXBfc3RyaW5ncyhjaGFyKiBhLCBjaGFyKiBiKQp7CiAgICB1bnNpZ25lZCBpID0gMCwgaiA9IDA7CiAgICB1bnNpZ25lZCBlb3NfY291bnQgPSAwOyAvLyBlbmQtb2Ytc3RyaW5nIGNvdW50LgoKICAgIHdoaWxlIChlb3NfY291bnQgPCAyKQogICAgewogICAgICAgIHN3YXAoYVtpXSwgYltqXSk7CgogICAgICAgIGlmICghYVtpXSkKICAgICAgICAgICAgKytlb3NfY291bnQ7CgogICAgICAgIGlmICghYltqXSkKICAgICAgICAgICAgKytlb3NfY291bnQ7CgogICAgICAgICsraSwgKytqOwogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIGNoYXIgb25lWzMyXSA9ICJvbmUiOwogICAgY2hhciB0aHJlZVszMl0gPSAidGhyZWUiOwoKICAgIHN0ZDo6Y291dCA8PCAib25lOiBcIiIgPDwgb25lIDw8ICJcIlxuIjsKICAgIHN0ZDo6Y291dCA8PCAidGhyZWU6IFwiIiA8PCB0aHJlZSA8PCAiXCJcbiI7CgogICAgc3dhcF9zdHJpbmdzKG9uZSwgdGhyZWUpOwoKICAgIHN0ZDo6Y291dCA8PCAib25lOiBcIiIgPDwgb25lIDw8ICJcIlxuIjsKICAgIHN0ZDo6Y291dCA8PCAidGhyZWU6IFwiIiA8PCB0aHJlZSA8PCAiXCJcbiI7Cn0=