C 语言中交换字符串的程序
实施
现在,我们将看到程序的实际实施 −
#include <stdio.h> int main() { char s1[] = "TajMahal"; char s2[] = "Dazzling"; char ch; int index = 0; //Character by Character approach printf("Before Swapping - "); printf("Value of s1 - %s ", s1); printf("Value of s2 - %s ", s2); while(s1[index] != '\0') { ch = s1[index]; s1[index] = s2[index]; s2[index] = ch; index++; } printf("After Swapping - "); printf("Value of s1 - %s ", s1); printf("Value of s2 - %s ", s2); return 0; }
输出
此程序的输出应为 −
Before Swapping - Value of s1 - TajMahal Value of s2 - Dazzling After Swapping - Value of s1 - Dazzling Value of s2 - TajMahal