#include <iostream>
// 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)
{
char c = a[i] ;
a[i] = b[j] ;
b[j] = c ;
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";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKLy8gYXNzdW1lcyBib3RoIGEgYW5kIGIgcG9pbnQgdG8gZW5vdWdoIG1lbW9yeSB0byBob2xkIHRoZSAKLy8gbG9uZ2VzdCBvZiB0aGUgc3RyaW5ncy4Kdm9pZCBzd2FwX3N0cmluZ3MoY2hhciBhW10sIGNoYXIgYltdKQp7CiAgICB1bnNpZ25lZCBpID0gMCwgaiA9IDA7CiAgICB1bnNpZ25lZCBlb3NfY291bnQgPSAwOyAvLyBlbmQtb2Ytc3RyaW5nIGNvdW50LgoKICAgIHdoaWxlIChlb3NfY291bnQgPCAyKQogICAgewogICAgICAgIGNoYXIgYyA9IGFbaV0gOwogICAgICAgIGFbaV0gPSBiW2pdIDsKICAgICAgICBiW2pdID0gYyA7CgogICAgICAgIGlmICghYVtpXSkKICAgICAgICAgICAgKytlb3NfY291bnQ7CgogICAgICAgIGlmICghYltqXSkKICAgICAgICAgICAgKytlb3NfY291bnQ7CgogICAgICAgICsraSwgKytqOwogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIGNoYXIgb25lWzMyXSA9ICJvbmUiOwogICAgY2hhciB0aHJlZVszMl0gPSAidGhyZWUiOwoKICAgIHN0ZDo6Y291dCA8PCAib25lOiBcIiIgPDwgb25lIDw8ICJcIlxuIjsKICAgIHN0ZDo6Y291dCA8PCAidGhyZWU6IFwiIiA8PCB0aHJlZSA8PCAiXCJcbiI7CgogICAgc3dhcF9zdHJpbmdzKG9uZSwgdGhyZWUpOwoKICAgIHN0ZDo6Y291dCA8PCAib25lOiBcIiIgPDwgb25lIDw8ICJcIlxuIjsKICAgIHN0ZDo6Y291dCA8PCAidGhyZWU6IFwiIiA8PCB0aHJlZSA8PCAiXCJcbiI7Cn0=