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