fork download
  1. #include <stdio.h>
  2.  
  3. template <size_t N>
  4. struct StrN
  5. {
  6. char str[N];
  7.  
  8. static void Swap(char *p0, char *p1)
  9. {
  10. StrN &s0 = *(StrN *)p0;
  11. StrN &s1 = *(StrN *)p1;
  12. StrN tmp = s0;
  13. s0 = s1;
  14. s1 = tmp;
  15. }
  16. };
  17.  
  18.  
  19. #define SWAP10(STR, IDX0, IDX1) StrN<10>::Swap(&STR[IDX0 * 10], &STR[IDX1 * 10])
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25. char str[21] = {"abcdefghijABCDEFGHIJ"};
  26.  
  27. printf("Before swapping :\n%s\n\n", str);
  28.  
  29. SWAP10(str, 0, 1);
  30.  
  31. printf("After swapping :\n%s\n\n", str);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
Before swapping :
abcdefghijABCDEFGHIJ

After swapping :
ABCDEFGHIJabcdefghij