헤더 파일
<string.h>
사용 목적
문자열의 길이를 조사 (마지막 NULL은 길이에 들어가지 않는다)
프로토타입과 매개변수
size_t strlen(const char *str);
1. const char *str = 길이를 조사할 문자열
함수의 원리
이해를 돕기 위해 같은 동작을 하는 함수를 만든 것입니다.
size_t ft_strlen(const char *str)
{
size_t count;
count = 0;
while (*str) // str이 NULL이 나오기 전까지
{
str++;
count++; // count를 늘려나감
}
return (count); // count를 반환
}
사용 예시
#include <string.h>
#include <stdio.h>
int main(void)
{
char *str = "abcde";
int length;
length = strlen(str);
printf("length of str = %d\n", length);
}
length of str = 5
'C언어 > 함수' 카테고리의 다른 글
memset (0) | 2020.09.25 |
---|---|
memmove (0) | 2020.08.10 |
memccpy (0) | 2020.08.10 |
memcpy (0) | 2020.08.10 |
memchr (0) | 2020.08.10 |
memcmp (0) | 2020.08.10 |