其他分享
首页 > 其他分享> > 【C语言】strcmp模拟实现

【C语言】strcmp模拟实现

作者:互联网

Introduction

strcmp
int strcmp ( const char * str1, const char * str2 );
Compare two strings
Compares the C string str1 to the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.

Parameters
str1
C string to be compared.
str2
C string to be compared.

在这里插入图片描述

Realization

int my_strcmp(
	const char* str1, 
	const char* str2
	)
{
	assert(str1 && str2);
	int ret = 0;
	while ((ret = (*str1 - *str2)) == 0 && *str1)
		{
			str1++;
			str2++;
		}
	return -(ret < 0) + (ret > 0);
}

标签:const,string,str2,str1,ret,C语言,char,strcmp,模拟
来源: https://blog.csdn.net/m0_52640673/article/details/120634720