C/C++ 字串比較(比對)
C/C++ 字串比較(比對)
資料來源:https://mp.weixin.qq.com/s/lZ9SsObIMoncjs4ZoyLRlA
線上編譯:https://www.tutorialspoint.com/compile_c_online.php
Code
#include <stdio.h> #include <string.h> int sky_strcmp(char *dst, char *str)//一致回傳1,其他回傳0 { int i, len; if (NULL == dst || NULL == str) { return 0; } if (strlen(dst) != strlen(str)) { return 0; } len = strlen(dst); for (i = 0; i < len; i++) { if (*dst++ != *str++) { return 0; } } return 1; } int main() { char str_1[100] = "hello,world"; char str_2[100] = "hello,world"; char str[100] = "adfs"; printf("%d\n",sky_strcmp(str_1,str)); printf("%d\n",sky_strcmp(str_1,str_2)); return 0; }