

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C function to Swap strings
The following is an example to swap strings.
Example
#include<stdio.h> #include <string.h> int main() { char st1[] = "My 1st string"; char st2[] = "My 2nd string"; char swap; int i = 0; while(st1[i] != '\0') { swap = st1[i]; st1[i] = st2[i]; st2[i] = swap; i++; } printf("After swapping s1 : %s\n", st1); printf("After swapping s2 : %s\n", st2); return 0; }
Output
After swapping s1 : My 2nd string After swapping s2 : My 1st string
In the above program, two arrays of char type st1 and st2, a char variable ‘swap’ and an integer variable i are declared. While loop is checking if st1 is not null, swap the values of st1 and st2.
char st1[] = "My 1st string"; char st2[] = "My 2nd string"; char swap; int i = 0; while(st1[i] != '\0') { swap = st1[i]; st1[i] = st2[i]; st2[i] = swap; i++; }
- Related Questions & Answers
- C program to swap two strings
- swap() function in C++
- unordered_multimap swap() function in C++ STL
- multimap swap() function in C++ STL
- Swap two Strings without using temp variable in C#
- C++ program to get length of strings, perform concatenation and swap characters
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- C++ Program to Swap Two Numbers
- Swap two Strings without using third user defined variable in Java
- Swap two numbers in C#
- list swap( ) in C++ STL
- forward_list::swap( ) in C++ STL
- queue::swap() in C++ STL
- multimap::swap() in C++ STL
- stack swap() in C++ STL
Advertisements