Program to swap string in C

Swap string means exchange their string. We can do it using two string. We will take two string from user then interchange the two string using C program. Let’s go to the program bellow which will swap two string.

C program code to swap string

// c program to swap two string

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

int main(){
  char first_string[200], second_string[200], temp_string[200];

  printf("Enter first string here : \n");

  printf("\nEnter second string here : \n");

  printf("\nYou have entered the following string : \n");
  printf("String 1 : %s\n", first_string);
  printf("String 2 : %s\n", second_string);

  strcpy(temp_string, first_string);
  strcpy(first_string, second_string);
  strcpy(second_string, temp_string);

  printf("\nYour string after swapping is : \n");
  printf("String 1 : %s\n", first_string);
  printf("String 2 : %s\n", second_string);

  return 0;

Output of swap string program:


To learn details about C string see our C string guide and try to make several program using string.h header file. This will increase you ability to work with string.

Previous pageRemove spaces

Next pageRandom numbers