Tuesday, September 18, 2012

Code# 86:c program to swap two strings

| |
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>

main()
{
   char first[100], second[100], *temp;

   printf("Enter the first string ");
   gets(first);

   printf("Enter the second string ");
   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);

   getch();
   return 0;
}

Output:
Enter the first string i have no girlfriend
Enter the second string i have no boyfriend

Before Swapping
First string: i have no girlfriend
Second string: i have no boyfriend

After Swapping
First string: i have no boyfriend
Second string: i have no girlfriend

0 comments:

Post a Comment

Powered by Blogger.