C03

김원호·2023년 7월 25일

42seoul

목록 보기
6/10

Exercise 00 : ft_strcmp

int	ft_strcmp(char *s1, char *s2)
{
	int				i;
	int				result;
	unsigned char	*str1;
	unsigned char	*str2;

	i = 0;
	result = 0;
	str1 = (unsigned char *)s1;
	str2 = (unsigned char *)s2;
	while (1)
	{
		result = str1[i] - str2[i];
		if (result != 0)
			break ;
		if (str1[i] == '\0' || str2[i] == '\0')
			break ;
		i++;
	}
	return (result);
}

string compare 즉 문자열 두 개를 비교해주는 함수
return값에 대한 설명이 0일 때 같고 0보다 작다와 크다라고 나와서 헷갈렸지만 다행히 두 문자열이 만나서 처음으로 다른(차이가 0이 아닌) 문자가 나왔을 때 그 차이를 리턴해주면 된다.

  • 주의할점 : 확장 ASCII

Exercise 01 : ft_strncmp

int	ft_strncmp(char *s1, char *s2, unsigned int n)
{
	unsigned int	i;
	int				result;
	unsigned char	*str1;
	unsigned char	*str2;

	i = 0;
	result = 0;
	str1 = (unsigned char *)s1;
	str2 = (unsigned char *)s2;
	if (n == 0)
		return (0);
	while (i < n)
	{
		result = str1[i] - str2[i];
		if (result != 0)
			break ;
		if (str1[i] == '\0' || str2[i] == '\0')
			break ;
		i++;
	}
	return (result);
}

00번과 마찬가지, 하지만 비교할 길이를 정해준다.

Exercise 02 : ft_strcat

char	*ft_strcat(char *dest, char *src)
{
	int	i;
	int	j;

	i = 0;
	while (dest[i] != '\0')
		i++;
	j = 0;
	while (src[j] != '\0')
		dest[i++] = src[j++];
	dest[i] = '\0';
	return (dest);
}

string concatenate 즉 문자열을 이어주는 함수

Exercise 03 : ft_strncat

char	*ft_strncat(char *dest, char *src, unsigned int nb)
{
	unsigned int	i;
	unsigned int	j;

	i = 0;
	while (dest[i] != '\0')
		i++;
	j = 0;
	while (src[j] != '\0' && j < nb)
		dest[i++] = src[j++];
	dest[i] = '\0';
	return (dest);
}

02번과 마찬가지, 하지만 src를 최대 nb까지 붙여준다.

Exercise 04 : ft_strstr

char	*ft_strstr(char *str, char *to_find)
{
	int		check;
	int		i;
	int		j;

	i = 0;
	if (!(to_find))
		return (str);
	while (str[i] != '\0')
	{
		check = 1;
		j = 0;
		while (to_find[j] != '\0')
		{
			if (str[i + j] != to_find[j] || str[i] == '\0')
			{
				check = 0;
				break ;
			}
			j++;
		}
		if (check)
			return (&str[i]);
		i++;
	}
	return (0);
}

string 안에 특정 string이 존재하는지 확인하는 함수
함수를 탐색하면서 to_find보다 str이 먼저 null에 도달하면 안되기 때문에 to_find의 인덱스를 증가시키는 조건에 str이 null인지를 판단하지 않고, 내부에서 일치하지 않는 조건을 판단할때 str이 null인지 확인.

Exercise 05 : ft_strlcat

unsigned int	ft_strlen(char *str)
{
	unsigned int	count;

	count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return (count);
}

unsigned int	ft_strlcat(char *dest, char *src, unsigned int size)
{
	unsigned int	i;
	unsigned int	dest_len;
	unsigned int	src_len;

	i = 0;
	dest_len = ft_strlen(dest);
	src_len = ft_strlen(src);
	while (src[i] != '\0' && dest_len + i + 1 < size)
	{
		dest[dest_len + i] = src[i];
		i++;
	}
	dest[dest_len + i] = '\0';
	if (dest_len < size)
		return (src_len + dest_len);
	else
		return (src_len + size);
}

가장 이상하고 이해하기 어려웠던 함수
strcat처럼 두 string을 이어붙이는건 동일하지만, 매개변수에 정수가 들어가고 리턴값 또한 정수이다. 이 두 정수가 의미하는게 특이한데 결과적으로

  • return : src_len + min(dest_len, size)
  • 즉 붙이고자 하는 길이에 size 혹은 dest_len 중 작은 값을 더한 길이를 반환한다.

case 1 : dest_len > size
원래 길이가 size보다 더 큰 경우인데 그럴 때마다 항상 src_len과 size의 합이 반환된다. 이 값은 dest가 어떤 문자열인지에 상관 없이 같은 값이 반환된다. 따라서 만약 strlcat의 리턴값이 src_len + size인 경우에 "dest뒤에 src가 한글자도 반환되지 못했다" 라고 판단할 수 있겠다.

case 2 : dest_len < size
원래 길이보다 size가 더 큰 경우인데 src_len과 size를 알고있다면, 앞서 말한 case 1보다 작은 값이 반환될 것이다. 즉 그 값을 보고 "dest에 src 일부가 붙었구나" 라고 판단할 수 있다.

최종적으로 보면 dest에 src가 붙은 문자열의 길이는 null문자를 포함하여 size를 초과할 수 없다. 즉 size라는 변수값과 리턴값을 통해서 이어붙인 문자열의 길이와 문자열이 문자열 뒤에 붙었는지 아닌지를 확인할 수 있게 된다.

0개의 댓글