程序来比较C中的两个字符串(Program to compare two strings in C)

优质
小牛编辑
133浏览
2023-12-01

实现 (Implementation)

现在,我们将看到该计划的实际执行情况 -

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

输出 (Output)

该计划的输出应为 -

advise and advice are NOT identical