C program to swap strings.

 

01 #include<stdio.h>
02 #include<string.h>
03 #include<malloc.h>
04
05 int main()
06 {
07 char first[100], second[100], *temp;
08 printf("Enter the first string ");
09 gets(first);
10
11 printf("Enter the second string ");
12 gets(second);
13
14 printf("nBefore Swappingn");
15
16 printf("First string: %sn",first);
17 printf("Second string: %snn",second);
18 temp = (char*)malloc(100);
19 strcpy(temp,first);
20 strcpy(first,second);
21 strcpy(second,temp);
22
23 printf("After Swappingn");
24 printf("First string: %sn",first);
25 printf("Second string: %sn",second);
26
27 return 0;
28 }
 
 
OUTPUT :

Enter the first string Apple
Enter the second string Ball

Before Swapping
First string: Apple
Second string: Ball

After Swapping
First string: Ball
Second string: Apple

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.