ft_strncmp.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ft_strncmp.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: lfiorell <lfiorell@student.42nice.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2024/07/22 17:48:55 by lfiorell #+# #+# */ 9 /* Updated: 2024/07/22 18:21:25 by lfiorell ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 int ft_strncmp(char *s1, char *s2, unsigned int n) 14 { 15 unsigned int i; 16 17 i = 0; 18 while (s1[i] && s2[i] && s1[i] == s2[i] && i < n) 19 { 20 i++; 21 } 22 return ((unsigned char)s1[i] - (unsigned char)s2[i]); 23 } 24 25 /* 26 #include <assert.h> 27 #include <stdio.h> 28 29 void test_ft_strcmp(void) 30 { 31 char *test1a; 32 char *test1b; 33 char *test2a; 34 char *test2b; 35 char *test3a; 36 char *test3b; 37 char *test4a; 38 char *test4b; 39 char *test5a; 40 char *test5b; 41 char *test6a; 42 char *test6b; 43 char *test7a; 44 char *test7b; 45 char *test8a; 46 char *test8b; 47 48 test1a = "furpaws"; 49 test1b = "furpaws"; 50 assert(ft_strncmp(test1a, test1b, 7) == 0); 51 printf("Test 1: Passed! They're both totally fur-tastic!\n"); 52 test2a = "furball"; 53 test2b = "furballz"; 54 assert(ft_strncmp(test2a, test2b, 7) < 0); 55 printf("Test 2: Passed! But our paws are a bit off!\n"); 56 test3a = "pawprint"; 57 test3b = "pawprince"; 58 assert(ft_strncmp(test3a, test3b, 6) == 0); 59 printf("Test 3: Passed! Both strings are purrfectly aligned up to the 7th " 60 "char!\n"); 61 test4a = "fluff"; 62 test4b = "fluffy"; 63 assert(ft_strncmp(test4a, test4b, 4) == 0); 64 printf("Test 4: Passed! These strings are just " 65 "like a comfy blanket of fur!\n"); 66 test5a = "furries"; 67 test5b = "furry"; 68 assert(ft_strncmp(test5a, test5b, 6) < 0); 69 printf("Test 5: Passed! The extra 'i' is totally fluff-tastic!\n"); 70 test6a = "tail"; 71 test6b = "tale"; 72 assert(ft_strncmp(test6a, test6b, 4) < 0); 73 printf("Test 6: Passed! A tale as old as time, or should I say a tail?\n"); 74 test7a = "snuggle"; 75 test7b = "snug"; 76 assert(ft_strncmp(test7a, test7b, 7) > 0); 77 printf("Test 7: Passed! We’ve got extra snuggles here!\n"); 78 test8a = "whisker"; 79 test8b = "whiskers"; 80 assert(ft_strncmp(test8a, test8b, 7) < 0); 81 printf("Test 8: Passed! Just a whisker away from being identical!\n"); 82 } 83 84 int main(void) 85 { 86 test_ft_strcmp(); 87 return (0); 88 } 89 */