You are here

C program to swap two strings

C program to swap two strings, i.e., contents of the two strings are interchanged.

C programming code

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.   char first[100], second[100], temp[100];
  7.  
  8.   printf("Enter first string\n");
  9.   gets(first);
  10.  
  11.   printf("Enter second string\n");
  12.   gets(second);
  13.  
  14.   printf("\nBefore Swapping\n");
  15.   printf("First string: %s\n", first);
  16.   printf("Second string: %s\n\n", second);
  17.  
  18.   strcpy(temp, first);
  19.   strcpy(first, second);
  20.   strcpy(second, temp);
  21.  
  22.   printf("After Swapping\n");
  23.   printf("First string: %s\n", first);
  24.   printf("Second string: %s\n", second);
  25.  
  26.   return 0;
  27. }

Download Swap strings program.

Output of program:
Swap strings C program output