c语言练习43:深入理解strcmp

深入理解strcmp

 strcmp的主要功能是用来比较两个字符串

模拟实现strcmp

比较两个字符串对应位置上的大小

按字典序进行比较

例如:

输入:abc abc

输出:0

输入:abc ab

输出:>0的数

输入:ab abc

输出:<0的数

扫描二维码关注公众号,回复: 16753232 查看本文章
//深入理解strcmp
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* s1, const char* s2) {
	assert(s1 != NULL);
	assert(s2 != NULL);
	while (*s1 == *s2) {
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	return *s1 - *s2;
}
int main() {
	int ret=my_strcmp("abc","abc");
	printf("%d\n", ret);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/2301_77479435/article/details/132753242