➰ 코딩 부트캠프/42 seoul

[0 Circle] Libft - ft_memcmp, ft_strlen

 사과개발자 2020. 12. 23. 19:50

📌ft_memcmp

str1과 str2를 n바이트만큼 비교

void    *ft_memcmp(const void *str1, const void *str2, unsigned int n);

➕ 매개변수 (Parameters)

  • str1 : 비교할 첫번째 메모리의 시작값
  • str2 : 비교할 두번째 메모리의 시작값
  • n : 비교할 만큼의 바이트 수

➕ 반환값 (Return)

  • int : str2가 str1보다 큰 경우 음수 / 작은 경우 양수 / 동일한 경우 0

➕ 설명 (Description)

➕ 코드 (Code)

 #include "libft.h" 

 int ft_memcmp(const void *str1, const void *str2, unsigned int n) 
 { 
     unsigned const char *tmp1; 
     unsigned const char *tmp2; 
     unsigned int        i; 

     i = 0; 
     tmp1 = str1; 
     tmp2 = str2; 

     if (!n) 
         return (0); 

     while (tmp1[i] == tmp2[i] && i < (n - 1)) 
     { 
         if (tmp1[i] != tmp2[i]) 
             return ((int)tmp1[i] - (int)tmp2[i]); 
         i++; 
     } 

     if (tmp1[i] == tmp2[i]) 
         return (0); 

     return ((int)tmp1[i] - (int)tmp2[i]); 
 }

📌ft_strlen

str의 길이를 반환

unsigned int ft_strlen(const char *str));

➕ 매개변수 (Parameters)

  • str : 길이를 계산할 문자열

➕ 반환값 (Return)

  • unsigned int : 문자열의 길이

➕ 설명 (Description)

➕ 코드 (Code)

 #include "libft.h" 

 unsigned intft_strlen(const char *str) 
 { 
     unsigned int    i; 

     i = 0; 
     while (str[i]) 
         i++; 
     return (i); 
 }
반응형