-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strncmp.c
More file actions
48 lines (45 loc) · 1.62 KB
/
Copy pathft_strncmp.c
File metadata and controls
48 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjurkows <kjurkows@student.42warsaw.pl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/16 14:21:22 by kjurkows #+# #+# */
/* Updated: 2026/06/29 23:37:44 by kjurkows ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//# Part 1
//## string manipulation
/** @brief compare two strings
*
* compares only first non-matching character.
* operates on ASCII codes
*
* @param s1 first string
* @param s2 second string
* @param n max length for comparison
* @return difference between first unmatched chars
* @retval 0 no difference
*/
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
const unsigned char *str1 = (const unsigned char*)s1;
const unsigned char *str2 = (const unsigned char*)s2;
size_t i;
i = 0;
if (!s1 && s2)
return (-str2[i]);
if (!s2 && s1)
return (str1[i]);
if (!s1 && !s2)
return (0);
while ((str1[i] || str2[i]) && i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}