Sunday, 24 August 2014

Filled Under: ,

C program to swap two string

Share
Here is simple C program to swap two string is given below.It takes two string using gets function and using a build in  function strcpy() ,which is use to copy the string in variable.
C program to swap two string


C program to swap two string


#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
int main()
{
   char String1[100], String2[100], *temp;
 
   printf("Enter the first string\n");
   gets(String1);
 
   printf("Enter the second string\n");
   gets(String2);
 
   printf("\nBefore Swapping\n");
   printf("First string: %s\n",String1);
   printf("Second string: %s\n\n",String2);
 
   temp = (char*)malloc(100);
 
   strcpy(temp,String1);
   strcpy(String1,String2);
   strcpy(String2,temp);
 
   printf("After Swapping\n");
   printf("First string: %s\n",String1);
   printf("Second string: %s\n",String2);
 
   return 0;
}


Output

C program to swap two string