- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
Program to Swap Strings in C
Implementation
Now, we shall see the actual implementation of the program −
#include <stdio.h> int main() { char s1[] = "TajMahal"; char s2[] = "Dazzling"; char ch; int index = 0; //Character by Character approach printf("Before Swapping - \n"); printf("Value of s1 - %s \n", s1); printf("Value of s2 - %s \n", s2); while(s1[index] != '\0') { ch = s1[index]; s1[index] = s2[index]; s2[index] = ch; index++; } printf("After Swapping - \n"); printf("Value of s1 - %s \n", s1); printf("Value of s2 - %s \n", s2); return 0; }
Output
Output of this program should be −
Before Swapping - Value of s1 - TajMahal Value of s2 - Dazzling After Swapping - Value of s1 - Dazzling Value of s2 - TajMahal
string_programs_in_c.htm
Advertisements