C Program To Swap Two Strings

The following is a C Program To Swap Two Strings on screen after taking input from the user. Let's get started.

C Program To Swap Two Strings
C Program To Swap Two Strings

Overview:


Commands

gets() function is used to obtain input as a string and printf() function is used to print the string on the screen.
strcpy() is used to copy strings and ultimately swap them.

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char first[100], second[100], temp[100];
  clrscr();
  printf("Enter first string\n");
  gets(first);
  printf("Enter second string\n");
  gets(second);
  printf("\nBefore Swapping\n");
  printf("First string: %s\n",first);
  printf("Second string: %s\n\n",second);
  strcpy(temp,first);
  strcpy(first,second);
  strcpy(second,temp);
  printf("After Swapping\n");
  printf("First string: %s\n",first);
  printf("Second string: %s\n",second); 
  getch();
}


Output

Before and After Swapping
Before and After Swapping

All programs compiled in this post are compiled in AnDOSBox.

Books I Prefer:






I hope this article was helpful to understand C Program To Swap Two Strings! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.

0 Comments

Post a Comment

Read Comment Policy before commenting.