Dies ist eine alte Version des Dokuments!
EXERCISE 00: FT_STRCMP FILE: FT_STRCMP.C
int ft_strcmp(char *s1, char *s2) {
int i = 0;
while (s1[i] && s1[i] == s2[i])
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
int ft_strcmp(char *s1, char *s2) {
int i = 0; while (s1[i] && s1[i] == s2[i]) i++; return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
EXERCISE 01: FT_STRNCMP FILE: FT_STRNCMP.C
int ft_strncmp(char *s1, char *s2, unsigned int n) {
unsigned int i = 0;
if (n == 0)
return 0;
while (s1[i] && s2[i] && s1[i] == s2[i] && i < n - 1)
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
int ft_strncmp(char *s1, char *s2, unsigned int n) {
unsigned int i = 0; if (n == 0) return 0; while (s1[i] && s2[i] && s1[i] == s2[i] && i< n-1) i++; return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
EXERCISE 02: FT_STRCAT FILE: FT_STRCAT.C
char *ft_strcat(char *dest, char *src) {
int i = 0;
int j = 0;
while (dest[i])
i++;
while (src[j])
{
dest[i + j] = src[j];
j++;
}
dest[i + j] = '\0';
return dest;
}
EXERCISE 03: FT_STRNCAT FILE: FT_STRNCAT.C
char *ft_strncat(char *dest, char *src, unsigned int nb) {
unsigned int i = 0;
unsigned int j = 0;
while (dest[i])
i++;
while (src[j] && j < nb)
{
dest[i + j] = src[j];
j++;
}
dest[i + j] = '\0';
return dest;
}
EXERCISE 04: FT_STRSTR FILE: FT_STRSTR.C
char *ft_strstr(char *str, char *to_find) {
int i, j;
if (!to_find[0])
return str;
i = 0;
while (str[i])
{
j = 0;
while (str[i + j] && to_find[j] && str[i + j] == to_find[j])
j++;
if (!to_find[j])
return &str[i];
i++;
}
return 0;
} EXERCISE 05: FT_STRLCAT FILE: FT_STRLCAT.C
unsigned int ft_strlen(char *str) {
unsigned int len = 0;
while (str[len])
len++;
return len;
}
unsigned int ft_strlcat(char *dest, char *src, unsigned int size) {
unsigned int dest_len = ft_strlen(dest); unsigned int src_len = ft_strlen(src); unsigned int i = 0;
if (size <= dest_len)
return size + src_len;
while (src[i] && (dest_len + i) < size - 1)
{
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
return dest_len + src_len;
}
unsigned int ft_strlen(char *str) {
unsigned int len = 0; while (str[len]) len++; return len;
}