How to swap two strings in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    char s1[16] = "c", s2[16] = "c++", tmp[16] = "";
   
   strcpy(tmp, s1);
   strcpy(s1, s2);
   strcpy(s2, tmp);

   printf("s1 : %s\n", s1);
   printf("s2 : %s\n", s2);
}





/*
run:
  
s1 : c++
s2 : c
  
*/

 



answered Sep 23, 2021 by avibootz

Related questions

1 answer 126 views
126 views asked Sep 23, 2021 by avibootz
1 answer 179 views
1 answer 142 views
1 answer 181 views
181 views asked Dec 20, 2023 by avibootz
1 answer 111 views
111 views asked Apr 19, 2023 by avibootz
1 answer 114 views
114 views asked May 29, 2022 by avibootz
1 answer 150 views
...