<틀린 부분, 부족한 부분 언제든지 댓글 달아주세요. 아직 코린이입니다.>
you must code a set of functions that are either not included in the libc, or included in a different form. Some of these functions can be useful to write Part 1’s functions.
1. ft_substr
char *ft_substr(char const *s, unsigned int start, size_t len)
description
- Allocates (with malloc(3)) and returns a substring from the string ’s’.
- The substring begins at index ’start’ and is of maximum size ’len’.
return
- The substring.
- NULL if the allocation fails.
- len + 1만큼 할당
- s의 길이보다 start가 더 길면 할당한 배열에 null넣고 반환
2. ft_strjoin
char *ft_strjoin(char const *s1, char const *s2)
description
- Allocates (with malloc(3)) and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’.
return
- The new string.
- NULL if the allocation fails.
- s1, s2 둘 중 하나라도 없는 경우 : strdup함수 사용 문자열 할당 후 복사
- memcpy함수 통해서 복사
- strlcpy와 strlcat함수의 조합으로 사용해도 가능하다
3. ft_strtrim
char *ft_strtrim(char const *s1, char const *set)
description
- Allocates (with malloc(3)) and returns a copy of ’s1’ with the characters specified in ’set’ removed from the beginning and the end of the string.
return
- The trimmed string.
- NULL if the allocation fails.
- set이 null이면 strdup함수 사용
- strchr함수 통해 s1문자가 set안에 있으면 index 증가
- 끝에서 시작하는 index는 처음 시작하는 index보다 클 경우의 경우의 수를 넣어줬음
- 반환값은 substr을 통해서 반환
4. ft_split
char **ft_split(char const *s, char c)
description
- Allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must be ended by a NULL pointer.
return
- The array of new strings resulting from the split.
- NULL if the allocation fails.
- 제일 고통받았던 함수 🤢
- 함수 5개를 사용 (사용시 주의사항 : 함수 앞 static 붙이기 / 이유 : 해당 파일 안에서만 함수 사용하기 위해서)
- 첫번째, c를 기준으로 단어의 개수 세주는 함수 (on/off 기능 이용)
- 두번째, free함수 - External functs에 나와있어서 해줬음
- 세번째, 1차원 배열에 string 넣어주는 함수 - 생각해보니 Part.1에 strcpy가 없어서 만들어줬음
- 네번째, 단어의 처음과 끝 index 찾아주는 함수
- 다섯번째, 2차원 배열 만드는 함수 = ft_split
- Piscine 시험에서도 4시간 잡고 있었는데 현실에 와서도 엄청 오래 잡고 있었음...ㅜㅜ
5. ft_itoa
char *ft_itoa(int n)
description
- Allocates (with malloc(3)) and returns a string representing the integer received as an argument.
- Negative numbers must be handled.
return
- The string representing the integer.
-NULL if the allocation fails.
- 함수 3개 사용
- 첫번째, n의 길이 함수 - 10으로 나눠주면서 count 늘림
- 두번째, 배열에 숫자 하나씩 넣어주는 함수 (실질적인 itoa)
- 세번째, 두번째 함수를 하나에 넣다 보니 norminette에 걸려서 케이스 분리해줬음
- n = -2147483648일 때만 따로 케이스 나눴음
6. ft_strmapi
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
description
- Applies the function ’f’ to each character of the string ’s’ to create a new string (with malloc(3)) resulting from successive applications of ’f’.
return
- The string created from the successive applications of ’f’.
- Returns NULL if the allocation fails.
- (*f) : 함수 포인터
- 앞 char : 반환 형태 / 괄호 안 unsigned int, char : input 값
- 함수 포인터도 null일 수 있음
7. ft_putchar_fd
void ft_putchar_fd(char c, int fd)
description
- Outputs the character ’c’ to the given file descriptor.
- fd가 무엇인지에 대해서 알아야 하는 함수
- fd : File Descripter
- 기본 할당 fd
- 0 : Standard Input
- 1 : Standard Output
- 2 : Standard Error
- write함수 사용
8. ft_putstr_fd
void ft_putstr_fd(char *s, int fd)
description
- Outputs the string ’s’ to the given file descriptor.
- while문 사용
9. ft_putendl_fd
void ft_putendl_fd(char *s, int fd)
description
- Outputs the string ’s’ to the given file descriptor, followed by a newline.
- putstr이후 마지막 \n 출력
10. ft_putnbr_fd
void ft_putnbr_fd(int n, int fd)
description
- Outputs the integer ’n’ to the given file descriptor.
- Piscine때 했던 putnbr 함수에 fd 변수 추가
'코딩 > C' 카테고리의 다른 글
| Libft(Bonus Part) (0) | 2021.02.06 |
|---|---|
| Libft (Part 1 : Libc Function) (0) | 2020.12.30 |