#include <stdio.h>
#include <string.h>
#include <assert.h>
unsigned int len(char* str)
{
unsigned int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
int ft_size(int s1_len, int s2_len)
{
if (s1_len > s2_len)
return s1_len;
else
return s2_len;
}
int ft_strcmp(char* s1, char* s2)
{
unsigned int i;
unsigned int i_len;
i = 0;
i_len = ft_size(len(s1), len(s2));
while (i < i_len)
{
if (s1[i] == '\0' || s2[i] == '\0' || s1[i] != s2[i])
return s1[i] - s2[i];
i++;
}
return 0;
}
int main()
{
char str1_1[] = "hello";
char str2_1[] = "hello";
assert(ft_strcmp(str1_1, str2_1) == 0);
// Test Case 2: First string is lexicographically smaller
char str1_2[] = "apple";
char str2_2[] = "banana";
assert(ft_strcmp(str1_2, str2_2) < 0);
// Test Case 3: First string is lexicographically larger
char str1_3[] = "orange";
char str2_3[] = "grape";
assert(ft_strcmp(str1_3, str2_3) > 0);
// Test Case 4: Strings with different lengths, same prefix
char str1_4[] = "hello";
char str2_4[] = "hell";
assert(ft_strcmp(str1_4, str2_4) > 0);
// Test Case 5: Empty string comparison
char str1_5[] = "";
char str2_5[] = "";
assert(ft_strcmp(str1_5, str2_5) == 0);
// Test Case 6: Empty string and non-empty string
char str1_6[] = "";
char str2_6[] = "non-empty";
assert(ft_strcmp(str1_6, str2_6) < 0);
// Test Case 7: Non-empty string and empty string
char str1_7[] = "non-empty";
char str2_7[] = "";
assert(ft_strcmp(str1_7, str2_7) > 0);
// Test Case 8: Strings with special characters
char str1_8[] = "hello!";
char str2_8[] = "hello?";
assert(ft_strcmp(str1_8, str2_8) < 0);
// Test Case 9: Strings with numbers
char str1_9[] = "123";
char str2_9[] = "1234";
assert(ft_strcmp(str1_9, str2_9) < 0);
// Test Case 10: Strings with different cases
char str1_10[] = "Hello";
char str2_10[] = "hello";
assert(ft_strcmp(str1_10, str2_10) < 0);
// Test Case 11: Unicode characters
char str1_11[] = "こんにちは";
char str2_11[] = "こんばんは";
assert(ft_strcmp(str1_11, str2_11) < 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: