C Program to swap two Strings
Program
#include <stdio.h>
void main() {
int i = 0, j = 0, k = 0;
char str1[20], str2[20], temp[20];
printf("Enter first string:\t");
scanf(" %s", str1);
printf("Enter second string:\t");
scanf(" %s", str2);
printf("Before swaping the strings:\n");
printf("String 1:\t%s\n", str1);
printf("String 2:\t%s\n", str2);
while (str1[i] != '\0') {
temp[j++] = str1[i++];
}
temp[j] = '\0';
i = 0, j = 0;
while (str2[i] != '\0') {
str1[j++] = str2[i++];
}
str1[j] = '\0';
i = 0, j = 0;
while (temp[i] != '\0') {
str2[j++] = temp[i++];
}
str2[j] = '\0';
printf("After swaping the strings:\n");
printf("String 1:\t%s\n", str1);
printf("String 2:\t%s\n", str2);
}
Output
Enter first string: welcome
Enter second string: oodlescoop
Before swaping the strings:
String 1: welcome
String 2: oodlescoop
After swaping the strings:
String 1: oodlescoop
String 2: welcome