#include <stdio.h>
#include <assert.h>
int main()
{
char p[] = "abcdef";
char q[] = "abcd";
printf("%d\n",my_strcmp(p,q));
return 0;
}
int my_strcmp (char const *p,char const *q)
{
int i = 0;
assert((p!= NULL)&&(q!= NULL));/*<span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;">assert的作用是現計算表達式 expression ,如果其值爲假(即爲0),那麼它先向stderr打印一條出錯信息,然後通過調用 abort 來終止程序運行。*/</span>
for(i=0;*(p+i) == *(q+i);i++)
{
if(*(p+i) == '\0')
return 0;
}
if(p[i]<q[i])
return -1;
else
return 1;
}
字符串的比較
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.