C Piscine C 03
Piscine C 03 ex00 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strcmp (man strcmp). • Here’s how it should be prototyped :
int ft_strcmp(char *s1, char *s2);
• Воспроизвести поведение функции strcmp (man strcmp). • Вот как это должно быть объявлено:
int ft_strcmp(char *s1, char *s2);
Решение 1
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && (*s1 == *s2))
{
s1++;
s2++;
}
return (*s1 - *s2);
}Решение 2
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] && (s1[i] == s2[i]))
{
i++;
}
return (s1[i] - s2[i]);
}Решение 3
int ft_strcmp(char *s1, char *s2)
{
while (*s1++ == *s2++)
if (*s1 == '\0' && *s2 == '\0')
return (0);
return (*--s1 - *--s2);
}Решение 4
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i])
{
if (s1[i] == '\0' && s2[i] == '\0')
return (0);
i++;
}
return (s1[i] - s2[i]);
}Решение 5
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] || s2[i])
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
i++;
}
return (0);
}Решение 6
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] || s2[i])
{
if (s1[i] < s2[i])
return (-1);
if (s1[i] > s2[i])
return (1);
i++;
}
return (0);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 03 ex01 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strncmp (man strncmp). • Here’s how it should be prototyped :
int ft_strncmp(char *s1, char *s2, unsigned int n);
• Воспроизвести поведение функции strncmp (man strncmp) • Вот как это должно быть объявлено:
int ft_strncmp(char *s1, char *s2, unsigned int n);
Решение 1
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] || s2[i])
{
if (s1[i] < s2[i])
return (-1);
if (s1[i] > s2[i])
return (1);
i++;
}
return (0);
}Решение 2
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
int i;
i = 0;
while (i < n)
{
if (*s1 != *s2)
return (*s1 - *s2);
if (*s1 == '\0' && *s2 == '\0')
return (0);
*s1++;
*s2++;
i++;
}
return (0);
}Решение 3
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 1;
while (*s1++ == *s2++)
{
if (i >= n)
return (0);
if (*s1 == '\0' && *s2 == '\0')
return (0);
i++;
}
return (*--s1 - *--s2);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 03 ex02 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strcat (man strcat). • Here’s how it should be prototyped :
char *ft_strcat(char *dest, char *src);
Воспроизведите поведение функции strcat (man strcat). • Вот как это должно быть объявлено:
char *ft_strcat(char *dest, char *src);
Решение 1
char *ft_strcat(char *dest, char *src)
{
int i;
int dest_size;
i = 0;
dest_size = 0;
while (dest[dest_size])
dest_size++;
while (src[i])
{
dest[dest_size] = src[i];
dest_size++;
i++;
}
dest[dest_size] = '\0';
return (dest);
}Решение 2
char *ft_strcat(char *dest, char *src)
{
char *ptr;
ptr = dest;
while (*dest)
dest++;
while (*src)
*dest++ = *src++;
*dest = '\0';
return (ptr);
}Решение 3
char *ft_strcat(char *dest, char *src)
{
int i;
int lenght;
i = 0;
lenght = 0;
while (dest[i] != '\0')
{
lenght++;
i++;
}
i = 0;
while (src[i] != '\0')
{
dest[lenght + i] = src[i];
i++;
}
dest[lenght + i] = '\0';
return (dest);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 03 ex03 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strncat (man strncat). • Here’s how it should be prototyped :
char *ft_strncat(char *dest, char *src, unsigned int nb);
• Воспроизведите поведение функции strncat (man strncat). • Вот как это должно быть объявлено:
char *ft_strncat(char *dest, char *src, unsigned int nb);
Решение 1
char *ft_strncat(char *dest, char *src, int nb)
{
int i;
int dest_size;
i = 0;
dest_size = 0;
while (dest[dest_size] != '\0')
dest_size++;
while (i < nb && src[i] != '\0')
{
dest[dest_size] = src[i];
dest_size++;
i++;
}
dest[dest_size] = '\0';
return (dest);
}Решение 2
char *ft_strncat(char *dest, char *src, int nb)
{
int i;
int lenght;
i = 0;
lenght = 0;
while (dest[lenght] != '\0')
lenght++;
while (i < nb && src[i] != '\0')
{
dest[lenght] = src[i];
lenght++;
i++;
}
dest[lenght] = '\0';
return (dest);
}Решение 3
char *ft_strncat(char *dest, char *src, int nb)
{
int i;
int x;
i = 0;
x = 0;
while (dest[i] != '\0')
i++;
while (src[x] != '\0' && x < nb)
{
dest[i] = src[x];
i++;
x++;
}
dest[i] = '\0';
return (dest);
}Решение 4
char *ft_strncat(char *dest, char *src, int nb)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i])
i++;
while ((j < nb) && src[j])
{
dest[i + j] = src[j];
j++;
}
dest[i + j] = '\0';
return (dest);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 03 ex04 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strstr (man strstr). • Here’s how it should be prototyped :
char *ft_strstr(char *str, char *to_find);
• Воспроизвести поведение функции strstr (man strstr). • Вот как это должно быть объявлено:
char *ft_strstr(char *str, char *to_find);
Решение 1
char *ft_strstr(char *str, char *to_find)
{
int i;
int j;
i = 0;
if (to_find[0] == '\0')
return (str);
while (str[i])
{
j = 0;
while (str[i + j] == to_find[j])
{
if (to_find[j + 1] == '\0')
return (str + i);
j++;
}
i++;
}
return ((void *)0);
}Решение 2
char *ft_strstr(char *str, char *to_find)
{
char *a;
char *b;
b = to_find;
if (*b == '\0')
return (str);
while (*str)
{
if (*str == *b)
{
a = str;
while (*a == *b || *b == '\0')
{
if (*b == '\0')
return (str);
a++;
b++;
}
b = to_find;
}
str++;
}
return (0);
}Решение 3
char *ft_strstr(char *str, char *to_find)
{
int i;
int y;
char *occurrence;
occurrence = 0;
if (to_find[0] == '\0')
return (str);
i = 0;
while (str[i] != '\0')
{
if (str[i] == to_find[0])
{
occurrence = str + i;
y = 0;
while (str[i + y] == to_find[y])
{
if (to_find[y + 1] == '\0')
return (occurrence);
y++;
}
occurrence = 0;
}
i++;
}
return (0);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 03 ex04 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strlcat (man strlcat). • Here’s how it should be prototyped :
unsigned int ft_strlcat(char *dest, char *src, unsigned int size);
• Воспроизведите поведение функции strlcat (man strlcat). • Вот как это должно быть объявлено:
unsigned int ft_strlcat(char *dest, char *src, unsigned int size);
Решение 1
unsigned int ft_strlcat(char *dest, char *src, unsigned int size)
{
int i;
unsigned int dest_size;
i = 0;
dest_size = 0;
while (dest[dest_size])
dest_size++;
while (src[i] && (dest_size < (size - 1)))
{
dest[dest_size] = src[i];
dest_size++;
i++;
}
dest[dest_size] = '\0';
return (dest_size);
}Решение 2
static int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i += 1;
return (i);
}
unsigned int ft_strlcat(char *dst, char *src, unsigned int size)
{
int i;
int len;
i = 0;
len = 0;
while (dst[len] && len < size)
len++;
i = len;
while (src[len - i] && len + 1 < size)
{
dst[len] = src[len - i];
len++;
}
if (i < size)
dst[len] = '\0';
return (i + ft_strlen(src));
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out