English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example of swapping strings.
#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 result
After swapping s1 : My 2nd string After swapping s2 : My 1st string
In the above program, two char types are declared as st1and st2array, a char variable 'swap', and an integer variable i. In the loop to check st1is not null, swap st1and st2value.
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++; }