C语言比较字符串示例

在C语言中,如何比较字符串?创建一个源文件:program_to_compare_strings.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

上一篇: C语言字符串示例 下一篇: C语言数学计算程序