int strncmp(const char *string1, const char *string2, size_t count);
-1: string1 < string20: string1 = string21: string1 > string2
注意:一定要確保字串中有空字元”\0”
程式碼(指定長度內有不同)
此處指定比較長度為7
#include <stdio.h> #include <string.h> int main(void) { int result = 0; result = strncmp(string1,string2,7); printf("string1 compare with string2.Result:%d\n\n",result); result = strncmp(string2,string1,7); printf("string2 compare with string1.Result:%d\n\n",result); result = strncmp(string1,string1,7); printf("string1 compare with string1.Result:%d\n\n",result); return 0; }
此處指定比較長度為4
#include <stdio.h> #include <string.h> char string1[] = "string1"; char string2[] = "string2"; int main(void) { int result = 0; result = strncmp(string1,string2,4); printf("string1 compare with string2.Result:%d\n\n",result); result = strncmp(string2,string1,4); printf("string2 compare with string1.Result:%d\n\n",result); result = strncmp(string1,string1,4); printf("string1 compare with string1.Result:%d\n\n",result); return 0; }執行結果
沒有留言:
發佈留言