OneBite.Dev - Coding blog in a bite size

swap a string in C

Code snippet on how to swap a string in C

  void swap(char *x, char *y) 
  {
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
  }

The above code is used for swapping two strings. The function, swap(), takes two pointer variables (x and y) of type char as parameters. A temporary variable of type char, named temp, is declared. The value of the x pointer is stored in the temp variable. Next, the value of the y pointer is assigned to the x pointer. Finally, the value of the temp variable is assigned to the y pointer, resulting in the swapping of the two strings.

c