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

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

Cの文字列を比較するプログラム

実装

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

#include <stdio.h>

int main() {
   char s1[] = "advise";
   char s2[] = "advice";

   int n = 0;
   unsigned short flag = 1;

   while (s1[n] != '\0') {
      if(s1[n] != s2[n]) {
         flag = 0;
         break;
      }
      n++;
   }

   if(flag == 1) {
      printf("%s and %s are identical\n", s1, s2);
   } else {
      printf("%s and %s are NOT identical\n", s1, s2);
   }

   return 0;
}

出力

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

advise and advice are NOT identical