int strcmp(const char *string1, const char *string2);
功能:比較兩個字串
回傳:1,0,-1(據自己實驗下來,會依據第一個發現不同的字元進行比大小,並回傳結果)
回傳值代表意思:
-1: string1 < string20: string1 = string21: string1 > string2
注意:一定要確保字串中有空字元”\0”
範例程式碼
#include <stdio.h> #include <string.h> char string1[] = "string1"; char string2[] = "string2"; int main(void) { int result = 0; result = strcmp(string1,string2); printf("string1 compare with string2.Result:%d\n\n",result); result = strcmp(string2,string1); printf("string2 compare with string1.Result:%d\n\n",result); result = strcmp(string1,string1); printf("string1 compare with string1.Result:%d\n\n",result); return 0; }
沒有留言:
發佈留言