【C語言】 實現strcmp

#include <stdio.h>
#include <assert.h>

int my_strcmp(const char * str1, const char * str2)      
{
	assert(str1);
	assert(str2);
	while ((*str1 == *str2) && *str1 && *str2)
	{
		str1++;
		str2++;
	//while (!(*str1 && *str2))  //千萬不能這樣寫,假若一個爲0,則返回1 
	while (*str1 == 0 && *str2 == 0) //判斷str1和str是否同時指向 \0
		 return 1;       //相等返回1
	}
	return -1;           //不相等返回-1
}

void main()
{
       char str1[100] = {"i love"};
       char str2[50] = {"China "};
       printf("%d\n",my_strcmp(str1,str2));
}
相關文章
相關標籤/搜索