Author Topic: C program to swap two strings  (Read 1327 times)

candy

  • Full Member
  • ***
  • Posts: 146
    • View Profile
C program to swap two strings
« on: November 12, 2013, 01:08:24 PM »
C program to swap strings i.e. contents of two strings are interchanged.

C programming code
Code: [Select]
#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
int main()
{
   char first[100], second[100], *temp;
 
   printf("Enter the first string\n");
   gets(first);
 
   printf("Enter the second string\n");
   gets(second);
 
   printf("\nBefore Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);
 
   temp = (char*)malloc(100);
 
   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);
 
   return 0;
}



Engineer Forum

C program to swap two strings
« on: November 12, 2013, 01:08:24 PM »