#include <stdio.h>
#include <string.h>
#include <assert.h>
int ft_strncmp(char* s1, char* s2, unsigned int n)
{
unsigned int i;
unsigned int i_len;
i = 0;
if (n == 0)
return 0;
while (i < n)
{
if (s1[i] == '\0' || s2[i] == '\0' || s1[i] != s2[i])
return s1[i] - s2[i];
i++;
}
return 0;
}
int main() {
// Test Case 1: Identical strings with n less than length of the strings
char str1_1[] = "hello";
char str2_1[] = "hello";
assert(ft_strncmp(str1_1, str2_1, 3) == 0);
// Test Case 2: Identical strings with n greater than length of the strings
char str1_2[] = "hello";
char str2_2[] = "hello";
assert(ft_strncmp(str1_2, str2_2, 10) == 0);
// Test Case 3: First string is lexicographically smaller with n = 5
char str1_3[] = "apple";
char str2_3[] = "banana";
assert(ft_strncmp(str1_3, str2_3, 5) < 0);
// Test Case 4: First string is lexicographically larger with n = 6
char str1_4[] = "orange";
char str2_4[] = "grape";
assert(ft_strncmp(str1_4, str2_4, 6) > 0);
// Test Case 5: Strings with different lengths, same prefix, n less than common prefix length
char str1_5[] = "hello";
char str2_5[] = "hell";
assert(ft_strncmp(str1_5, str2_5, 3) == 0);
// Test Case 6: Strings with different lengths, same prefix, n more than common prefix length
char str1_6[] = "hello";
char str2_6[] = "hell";
assert(ft_strncmp(str1_6, str2_6, 5) > 0);
// Test Case 7: Empty string comparison with n = 0
char str1_7[] = "";
char str2_7[] = "";
assert(ft_strncmp(str1_7, str2_7, 0) == 0);
// Test Case 8: Empty string and non-empty string with n > 0
char str1_8[] = "";
char str2_8[] = "non-empty";
assert(ft_strncmp(str1_8, str2_8, 1) < 0);
// Test Case 9: Non-empty string and empty string with n > 0
char str1_9[] = "non-empty";
char str2_9[] = "";
assert(ft_strncmp(str1_9, str2_9, 1) > 0);
// Test Case 10: Strings with special characters with n = 6
char str1_10[] = "hello!";
char str2_10[] = "hello?";
assert(ft_strncmp(str1_10, str2_10, 6) < 0);
// Test Case 11: Strings with numbers with n = 3
char str1_11[] = "123";
char str2_11[] = "1234";
assert(ft_strncmp(str1_11, str2_11, 3) == 0);
// Test Case 12: Strings with different cases with n = 5
char str1_12[] = "Hello";
char str2_12[] = "hello";
assert(ft_strncmp(str1_12, str2_12, 5) < 0);
// Test Case 13: Unicode characters with n = 15 (or appropriate number based on encoding)
char str1_13[] = "こんにちは";
char str2_13[] = "こんばんは";
assert(ft_strncmp(str1_13, str2_13, 9) < 0);
printf("All tests passed.\n");
}
To embed this program on your website, copy the following code and paste it into your website's HTML: