Advertisement
dtung

print and swap strings with pointers

Aug 18th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void swap(char *p[], int i, int j) {
  4.     char *temp;
  5.     temp = p[i];
  6.     p[i] = p[j];
  7.     p[j] = temp;
  8. }
  9.  
  10. void print(int n, char *p[]) {
  11.     for (int i = 0; i < n; i++)
  12.         printf("%s ", p[i]);
  13.     printf("\n");
  14. }
  15.  
  16. int main() {
  17.     char test[4][15] = {"this", "is", "a", "test"};
  18.     char *p[4];
  19.     for (int i = 0; i < 4; i++) {
  20.         p[i] = test[i];
  21.     }
  22.  
  23.     print(4, p);
  24.     swap(p, 2, 3);
  25.     print(4, p);
  26.  
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement