C Piscine C 02
Piscine C 02 ex00 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strcpy (man strcpy). • Here’s how it should be prototyped :
char *ft_strcpy(char *dest, char *src);
• Воспроизведите поведение функции strcpy (man strcpy). • Вот как это должно быть объявлено:
char *ft_strcpy(char *dest, char *src);
Решение 1
char *ft_strcpy(char *dest, char *src) { int i; i = 0; while (src[i]) { dest[i] = src[i]; i++; } dest[i] = '\0'; return (dest); }
Решение 2
char *ft_strcpy(char *dest, char *src) { char *tmp; tmp = dest; while (*src) { *dest = *src; dest++; src++; } *dest = '\0'; return (tmp); }
Решение 3
char *ft_strcpy(char *dest, char *src) { char *t; t = dest; while ((*t++ = *src++) != 0) ; return (dest); }
Решение 4
char *ft_strcpy(char *dest, char *src) { int i; i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; return (dest); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex01 (ft_strcpy)
Задание:
• Reproduce the behavior of the function strncpy (man strncpy). • Here’s how it should be prototyped :
char *ft_strncpy(char *dest, char *src, unsigned int n);
• Воспроизвести поведение функции strncpy (man strncpy). • Вот как это должно быть объявлено:
char *ft_strncpy(char *dest, char *src, unsigned int n);
Решение 1
char *ft_strncpy(char *dest, char *src, unsigned int n) { unsigned int i; int size; size = 0; i = 0; while (i < n && src[i]) { dest[i] = src[i]; i++; } while (i < n) { dest[i] = '\0'; i++; } return (dest); }
Решение 2
char *ft_strncpy(char *dest, char *src, unsigned int n) { unsigned int i; i = 0; while ((src[i] != '\0') && (i < n)) { dest[i] = src[i]; i++; } while ((dest[i] != '\0') && (i < n)) { dest[i] = '\0'; i++; } return (dest); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex02 (ft_str_is_alpha)
Задание:
• Create a function that returns 1 if the string given as a parameter contains only alphabetical characters, and 0 if it contains any other character. • Here’s how it should be prototyped :
int ft_str_is_alpha(char *str);
• It should return 1 if str is empty.
• Создайте функцию, которая возвращает 1, если строка, заданная в качестве параметра, содержит только алфавитные символы, и 0, если она содержит еще и любой другой символ.
int ft_str_is_alpha(char *str);
• Функция должна возвращать 1, если str пуст(не содержит др символов).
Решение 1
int iss_alpha(char c) { if ((c >= 'a') && (c <= 'z')) return (1); if ((c >= 'A') && (c <= 'Z')) return (1); return (0); } int ft_str_is_alpha(char *str) { int i; i = 0; while (str[i]) { if (!(iss_alpha(str[i]))) return (0); i++; } return (1); }
Решение 2
int iss_alpha(char c) { return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))); } int ft_str_is_alpha(char *str) { int i; i = 0; while (str[i]) { if (!(iss_alpha(str[i]))) return (0); i++; } return (1); }
Решение 3
int ft_str_is_alpha(char *str) { int i; char c; i = 0; while (str[i] != '\0') { c = str[i]; if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) return (0); i++; } return (1); }
Решение 4
int ft_str_is_alpha(char *str) { int i; int b; b = 0; while (str[i] != '\0') { if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) b = 1; else return (0); i++; } return (b); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex03 (ft_str_is_numeric)
Задание:
• Create a function that returns 1 if the string given as a parameter contains only digits, and 0 if it contains any other character. • Here’s how it should be prototyped :
int ft_str_is_numeric(char *str);
• It should return 1 if str is empty.
• Создайте функцию, которая возвращает 1, если строка, заданная в качестве параметра, содержит только цифры, и 0, если она содержит любой другой символ.
int ft_str_is_numeric(char *str);
• Функция должна возвращать 1, если str пуст(не содержит др символов).
Решение 1
int iss_num(char c) { return ((c >= '0') && (c <= '9')); } int ft_str_is_numeric(char *str) { int i; i = 0; while (str[i]) { if (!(iss_num(str[i]))) return (0); i++; } return (1); }
Решение 2
int iss_num(char c) { if ((c >= '0') && (c <= '9')) return (1); return (0); } int ft_str_is_numeric(char *str) { int i; i = 0; while (str[i]) { if (!(iss_num(str[i]))) return (0); i++; } return (1); }
Решение 3
int ft_str_is_numeric(char *str) { int i; int b; b = 0; while (str[i] != '\0') { if (str[i] >= '0' && str[i] <= '9') b = 1; else return (0); i++; } return (b); }
Решение 4
int ft_str_is_numeric(char *str) { int i; i = 0; while (str[i] != '\0') { if (!(str[i] >= 48 && str[i] <= 57)) return (0); i++; } return (1); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex04 (ft_str_is_lowercase)
Задание:
• Create a function that returns 1 if the string given as a parameter contains only lowercase alphabetical characters, and 0 if it contains any other character. • Here’s how it should be prototyped :
int ft_str_is_lowercase(char *str);
• It should return 1 if str is empty.
Создайте функцию, которая возвращает 1, если строка, заданная в качестве параметра, содержит только строчные буквенные символы, и 0, если она содержит любой другой символ
int ft_str_is_lowercase(char *str);
• Функция должна возвращать 1, если строка 'str' пуста (не содержит др символов).
Решение 1
int ft_str_is_lowercase(char *str) { int i; i = 0; while (str[i]) { if (!((str[i] >= 'a') && (str[i] <= 'z'))) return (0); i++; } return (1); }
Решение 2
int iss_lowercase(char c) { if ((c >= 'a') && (c <= 'z')) return (1); return (0); } int ft_str_is_lowercase(char *str) { int i; i = 0; while (str[i]) { if (!(iss_lowercase(str[i]))) return (0); i++; } return (1); }
Решение 3
int ft_str_is_lowercase(char *str) { int i; int b; b = 0; while (str[i] != '\0') { if (str[i] >= 'a' && str[i] <= 'z') b = 1; else return (0); i++; } return (b); }
Решение 4
int ft_str_is_lowercase(char *str) { int i; i = 0; while (str[i] != '\0') { if (!(str[i] >= 97 && str[i] <= 122)) return (0); i++; } return (1); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex05 (ft_str_is_uppercase)
Задание:
• Create a function that returns 1 if the string given as a parameter contains only uppercase alphabetical characters, and 0 if it contains any other character. • Here’s how it should be prototyped :
int ft_str_is_uppercase(char *str);
• It should return 1 if str is empty.
• Создайте функцию, которая возвращает 1, если строка, заданная в качестве параметра, содержит только заглавные буквенные символы, и 0, если она содержит любой другой символ.
int ft_str_is_uppercase(char *str);
• Функция должна возвращать 1, если str пуст(не содержит др символов).
Решение 1
int ft_str_is_uppercase(char *str) { int i; i = 0; while (str[i]) { if (!((str[i] >= 'A') && (str[i] <= 'Z'))) return (0); i++; } return (1); }
Решение 2
int iss_uppercase(char c) { if ((c >= 'A') && (c <= 'Z')) return (1); return (0); } int ft_str_is_uppercase(char *str) { int i; i = 0; while (str[i]) { if (!(iss_uppercase(str[i]))) return (0); i++; } return (1); }
Решение 3
int ft_str_is_uppercase(char *str) { int i; int b; b = 0; while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') b = 1; else return (0); i++; } return (b); }
Решение 4
int ft_str_is_uppercase(char *str) { int i; i = 0; while (str[i] != '\0') { if (!(str[i] >= 65 && str[i] <= 90)) return (0); i++; } return (1); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex06 (ft_str_is_printable)
Задание:
• Create a function that returns 1 if the string given as a parameter contains only printable characters, and 0 if it contains any other character. • Here’s how it should be prototyped :
int ft_str_is_printable(char *str);
• It should return 1 if str is empty
• Создайте функцию, которая возвращает 1, если строка, заданная в качестве параметра, содержит только печатные символы, и 0, если она содержит любой другой символ.
int ft_str_is_printable(char *str);
• Функция должна возвращать 1, если строка str пуста(не содержит др символов).
Решение 1
int ft_str_is_printable(char *str) { int i; i = 0; while (str[i]) { if (!((str[i] >= 32) && (str[i] <= 126))) return (0); i++; } return (1); }
Решение 2
int iss_printable(char c) { return ((c >= 32) && (c <= 126)); } int ft_str_is_printable(char *str) { int i; i = 0; while (str[i]) { if (!(iss_printable(str[i]))) return (0); i++; } return (1); }
Решение 3
int ft_str_is_printable(char *str) { int i; int b; b = 0; while (str[i] != '\0') { if (str[i] >= ' ' && str[i] <= '~') b = 1; else return (0); i++; } return (b); }
Решение 4
int ft_str_is_printable(char *str) { int i; i = 0; while (str[i] != '\0') { if (!(str[i] >= 32 && str[i] <= 126)) return (0); i++; } return (1); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex07 (ft_strupcase)
Задание:
• Create a function that transforms every letter to uppercase. • Here’s how it should be prototyped :
char *ft_strupcase(char *str);
• It should return str
• Создать функцию, которая преобразует каждую букву каждого слова в верхний регистр. • Вот как это должно быть объявлено:
char *ft_strupcase(char *str);
•Функция должна вернуть строку.
Решение 1
char *ft_strupcase(char *str) { int i; i = 0; while (str[i]) { if ((str[i] >= 'a') && (str[i] <= 'z')) str[i] = ((str[i] - 'a') + 'A'); i++; } return (str); }
Решение 2
char *ft_strupcase(char *str) { int i; i = 0; while (str[i] != '\0') { if (str[i] >= 'a' && str[i] <= 'z') str[i] = ((str[i] - 'a') + 'A'); i++; } return (str); }
Решение 3
char *ft_strupcase(char *str) { int i; i = 0; while (str[i] != '\0') { if (str[i] >= 97 && str[i] <= 122) str[i] = str[i] - 32; i++; } return (str); }
Решение 4
char *ft_struppcase(char *str) { char *p; p = str; while (*p) { if ((*p >= 'a') && (*p <= 'z')) *p = ((*p - 'a') + 'A'); p++; } return (str); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex08 (ft_strupcase)
Задание:
• Create a function that transforms every letter to lowercase. • Here’s how it should be prototyped :
char *ft_strlowcase(char *str);
• It should return str.
• Создать функцию, которая преобразует каждую букву каждого слова в нижний регистр. • Вот как это должно быть объявлено:
char *ft_strlowcase(char *str);
• Функция должна вернуть строку.
Решение 1
char *ft_strlowcase(char *str) { int i; i = 0; while (str[i]) { if ((str[i] >= 'A') && (str[i] <= 'Z')) str[i] = str[i] + 32; i++; } return (str); }
Решение 2
char *ft_strlowcase(char *str) { int i; i = 0; while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') str[i] = str[i] - 'A' + 'a'; i++; } return (str); }
Решение 3
char *ft_strlowcase(char *str) { int i; i = 0; while (str[i] != '\0') { if (str[i] >= 65 && str[i] <= 90) str[i] = str[i] + 32; i++; } return (str); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex09 (ft_strupcase)
Задание:
• Create a function that capitalizes the first letter of each word and transforms all other letters to lowercase. • A word is a string of alphanumeric characters. • Here’s how it should be prototyped :
char *ft_strcapitalize(char *str);
• It should return str. • For example:
salut, comment tu vas ? 42mots quarante-deux; cinquante+et+un
• Becomes:
Salut, Comment Tu Vas ? 42mots Quarante-Deux; Cinquante+Et+U
• Создайте функцию, которая делает первую букву каждого слова заглавной и преобразует все остальные буквы в строчные. • Слово - это строка буквенно-цифровых символов. • Вот как это должно быть объявлено:
char *ft_strcapitalize(char *str);
• Функция должна вернуть строку.
• Например это:
salut, comment tu vas ? 42mots quarante-deux; cinquante+et+un
• Становится этим:
Salut, Comment Tu Vas ? 42mots Quarante-Deux; Cinquante+Et+U
Решение 1
int iss_alpha(char c) { if ((c >= 'a') && (c <= 'z')) return (1); if ((c >= 'A') && (c <= 'Z')) return (1); if ((c >= '0') && (c <= '9')) return (1); return (0); } int is_maj(char c) { return ((c >= 'A') && (c <= 'Z')); } char is_min(char c) { return ((c >= 'a') && (c <= 'z')); } char *ft_strcapitalize(char *str) { int i; i = 0; while (str[i]) { while (str[i] && !iss_alpha(str[i])) i++; if (str[i] && is_min(str[i])) str[i] = ((str[i] - 'a') + 'A'); i++; while (str[i] && iss_alpha(str[i])) { if (is_maj(str[i])) str[i] = ((str[i] - 'A') + 'a'); i++; } } return (str); }
Решение 2
int iss_alpha(char c) { if ((c >= 'a') && (c <= 'z')) return (1); if ((c >= 'A') && (c <= 'Z')) return (1); if ((c >= '0') && (c <= '9')) return (1); return (0); } char *ft_strcapitalize(char *str) { int i; i = 0; while (str[i]) { while (str[i] && !iss_alpha(str[i])) i++; if (str[i] && ((str[i] >= 'a') && (str[i] <= 'z'))) str[i] = ((str[i] - 'a') + 'A'); i++; while (str[i] && iss_alpha(str[i])) { if ((str[i] >= 'A') && (str[i] <= 'Z')) str[i] = ((str[i] - 'A') + 'a'); i++; } } return (str); }
Решение 3
int iss_alpha(char c) { if ((c >= 'a') && (c <= 'z')) return (1); if ((c >= 'A') && (c <= 'Z')) return (1); if ((c >= '0') && (c <= '9')) return (1); return (0); } int is_maj(char c) { return ((c >= 'A') && (c <= 'Z')); } char is_min(char c) { return ((c >= 'a') && (c <= 'z')); } char *ft_strcapitalize(char *str) { int i; int word_new; i = 0; word_new = 1; while (str[i]) { if (is_min(str[i]) && (word_new == 1)) str[i] = ((str[i] - 'a') + 'A'); else if (is_maj(str[i]) && (word_new == 0)) str[i] = ((str[i] - 'A') + 'a'); if (iss_alpha(str[i])) word_new = 0; else word_new = 1; i++; } return (str); }
Решение 4
char *jp_strlowcase(char *str) { int i; i = 0; while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') str[i] = ((str[i] - 'A') + 'a'); i++; } return (str); } char *ft_strcapitalize(char *str) { int i; i = 1; jp_strlowcase(str); if (str[0] >= 'a' && str[0] <= 'z') str[0] = ((str[0] - 'a') + 'A'); while (str[i]) { if ((str[i] >= ' ' && str[i] <= '/') || (str[i] >= ':' && str[i] <= '@')) if (str[i + 1] >= 'a' && str[i + 1] <= 'z') str[i + 1] = ((str[i + 1] - 'a') + 'A'); i++; } return (str); }
Решение 5
char *ft_strcapitalize(char *str) { int i; char c; int space; i = 0; while (str[i] != '\0') { space = 1; if (i == 0) c = ';'; else c = str[i - 1]; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) space = 0; if (c >= '0' && c <= '9') space = 0; if (space == 1 && str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] - 32; if (space == 0 && str[i] >= 'A' && str[i] <= 'Z') str[i] = str[i] + 32; i++; } return (str); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 02 ex10 (ft_strlcpy)
Задание:
• Reproduce the behavior of the function strlcpy (man strlcpy). • Here’s how it should be prototyped :
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size);
• Воспроизведите поведение функции strlcpy (man strlcpy).• Вот как это должно быть объявлено:
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size);
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size) { unsigned int i; i = 0; size -= 1; while (src[i] && (i < size)) { dest[i] = src[i]; i++; } dest[i] = '\0'; return (i); }
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out