Learn-c-by-examples-program-to-swap-strings-in-c

提供:Dev Guides
移動先:案内検索

Cで文字列を交換するプログラム

実装

今、私たちはプログラムの実際の実装が表示されます-

#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;
}

出力

このプログラムの出力は次のようになります-

Before Swapping -
Value of s1 - TajMahal
Value of s2 - Dazzling
After Swapping -
Value of s1 - Dazzling
Value of s2 - TajMahal