fork download
  1. #include <iostream>
  2.  
  3. void swap(char& a, char& b)
  4. {
  5. char c = a;
  6. a = b;
  7. b = c;
  8. }
  9.  
  10. // assumes both a and b point to enough memory to hold the
  11. // longest of the strings.
  12. void swap_strings(char* a, char* b)
  13. {
  14. unsigned i = 0, j = 0;
  15. unsigned eos_count = 0; // end-of-string count.
  16.  
  17. while (eos_count < 2)
  18. {
  19. swap(a[i], b[j]);
  20.  
  21. if (!a[i])
  22. ++eos_count;
  23.  
  24. if (!b[j])
  25. ++eos_count;
  26.  
  27. ++i, ++j;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. char one[32] = "one";
  34. char three[32] = "three";
  35.  
  36. std::cout << "one: \"" << one << "\"\n";
  37. std::cout << "three: \"" << three << "\"\n";
  38.  
  39. swap_strings(one, three);
  40.  
  41. std::cout << "one: \"" << one << "\"\n";
  42. std::cout << "three: \"" << three << "\"\n";
  43. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
one: "one"
three: "three"
one: "three"
three: "one"