ft_strncmp - gabrielcoelhodacunha-old/42sp-libft GitHub Wiki

Description

Compares len characters of the null terminated strings s1 and s2.

Implementation

int	ft_strncmp(const char *s1, const char *s2, size_t len)
{
	size_t	idx;

	if (!len)
		return (0);
	idx = 0;
	while (idx < (len - 1) && s1[idx] == s2[idx] && s1[idx])
		idx++;
	/* Returns the integer difference between the two strings at the last compared character.
	The result is explained as:
		1. positive value: `s1` is greater than `s2`
		2. zero: `s1` is equal to `s2`
		3. negative value: `s1` is lesser than `s2`
	*/
	return ((unsigned char) s1[idx]
		- (unsigned char) s2[idx]);
}

References