열렬히.뛰기

c02

école 42 > C > c02

  • 틀린문제 : 2, 9, 10, 12
  • 2번 : 범위가 잘못됨
  • 9번 : 조건에 맞지 않음.
  • 10번 :
  • 12번 :

ex00

c
char	*ft_strcpy(char *dest, char *src)
{
	int	x;

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

ex01

c
char	*ft_strncpy(char *dest, char *src, unsigned int n)
{
	unsigned int	x;

	x = 0;
	while (x < n && *src != '\0')
	{
		dest[x] = *src;
		x++;
		src++;
	}
	while (x < n)
	{
		dest[x] = '\0';
		x++;
	}
	return (dest);
}

ex02

c
int	ft_str_is_alpha(char *str)
{
	int	n;
	int	flag;

	n = 0;
	flag = 0;
	if (!*str)
	{
		flag = 1;
		return (flag);
	}
	while (str[n] != '\0')
	{
		if ((str[n] >= 'a' && str[n] <= 'z') || (str[n] >= 'A' && str[n] <= 'Z'))
			flag = 1;
		n++;
	}
	return (flag);
}
  • 범위를 잘 봐야 한다.
  • 또한 값이 없으면 1로 출력된다는 것도 잘 봐야 한다.

ex03

c
int	ft_str_is_numeric(char *str)
{
	while (*str != '\0')
	{
		if (!('0' <= *str && *str <= '9'))
			return (0);
		str++;
	}
	return (1);
}

ex04

c
int	ft_str_is_lowercase(char *str)
{
	if (!*str)
		return (1);
	while (*str != '\0')
	{
		if (!(97 <= *str && *str <= 122))
			return (0);
		str++;
	}
	return (1);
}

ex05

c
int	ft_str_is_uppercase(char *str)
{
	if (!*str)
		return (1);
	while (*str != '\0')
	{
		if (!(65 <= *str && *str <= 90))
			return (0);
		str++;
	}
	return (1);
}

ex06

c
int	ft_str_is_printable(char *str)
{
	if (!*str)
		return (1);
	while (*str != '\0')
	{
		if ((0 <= *str && *str <= 31) || *str == 127)
			return (0);
		str++;
	}
	return (1);
}

ex07, ex08

여기서는 배열을 이용해야 함.

포인터 = 문자열의 주소값이 고정됨.

배열 = 각각의 한 글자 한 글자를 인덱스로 넣을 수 있음.

*str = “hello” 바꿀 수 없음.

*str = hello 바꿀 수 있음

c
char	*ft_strupcase(char *str)
{
	int	n;

	n = 0;
	while (str[n] != '\0')
	{
		if (str[n] >= 97 && str[n] <= 122)
			str[n] -= 32;
		n++;
	}
	return (str);
}

ex09

c
int	is_lower(char ch)
{
	if (ch >= 'a' && ch <= 'z')
		return (1);
	else
		return (0);
}

int	is_alphanumeric(char ch)
{
	if (ch >= '0' && ch <= '9')
		return (1);
	else if (ch >= 'a' && ch <= 'z')
		return (1);
	else if (ch >= 'A' && ch <= 'Z')
		return (1);
	else
		return (0);
}

char	*ft_strcapitalize(char *str)
{
	int	i;
	int	j;

	i = 0;
	j = 1;
	if (str[0] >= 97 && str[0] <= 122)
	{
		str[0] -= 32;
	}
	while (str[j])
	{
		if (str[j] >= 65 && str[j] <= 90)
			str[j] += 32;
		j++;
	}
	while (str[i])
	{
		if (is_lower(str[i]) && !is_alphanumeric(str[i - 1]))
			str[i] -= 32;
		i++;
	}
	return (str);
}

ex10

c
unsigned int	ft_strlcpy(char *dest, char *src, unsigned int size)
{
	unsigned int	i_cnt;
	unsigned int	j;

	i_cnt = 0;
	j = 0;
	while (src[i_cnt] != '\0')
		i_cnt++;
	while (j + 1 < size)
	{
		dest[j] = src[j];
		j++;
	}
	if (size > 0)
		dest[j] = '\0';
	return (i_cnt);
}

ex11

c
#include <unistd.h>

void	hexadeci(unsigned char x)
{
	char	*hex;

	hex = "0123456789abcdef";
	write(1, "\\", 1);
	write(1, &hex[x / 16], 1);
	write(1, &hex[x % 16], 1);
}

int	is_or_not(unsigned char c)
{
	int	i;
	int	flag;

	i = 0;
	flag = 0;
	if (c >= 32 && c <= 126)
		flag = 1;
	return (flag);
}

void	ft_putstr_non_printable(char *str)
{
	int	n;

	n = 0;
	while (str[n] != '\0')
	{
		if (is_or_not((unsigned char)str[n]) == 1)
			write(1, &str[n], 1);
		else
			hexadeci((unsigned char)str[n]);
		n++;
	}
}
  • 주요 포인트 : 음수를 없애야 한다.
    • 따라서 자료형을 변환 : char to unsigned char
    • 16진수 변환 도중 ‘음수’번째 값이 나오면
    • 배열에서 적합한 값을 출력할 수 없다.
  • 16진수 변환을 알아야 한다.
    • 배열에 “0123456789abcedf”를 넣고 골라내자.
    • 숫자 x ⇒ x % 16 뽑고 변환 ⇒ x // 16 뽑고 변환
  • 왜 재귀가 없지?
    • 문자의 범위는 최대 122. 16으로 나눈 값이 두 자리 수가 나오지 않는다.
    • 따라서 재귀를 하지 않아도 충분하다.

ex12

c
#include <unistd.h>

void	address(unsigned long memory)
{
	char	arr[16];
	int		i;

	i = 15;
	while (i >= 0)
	{
		arr[i] = "0123456789abcdef"[memory % 16];
		i -= 1;
		memory /= 16;
	}
	write(1, &arr, 16);
	write(1, ":", 1);
}

void	change_hexde(unsigned char *addr, unsigned int size)
{
	unsigned char	i;

	i = 0;
	while (i < 16 && i < size)
	{
		if (i % 2 == 0)
			write(1, " ", 1);
		write(1, &"0123456789abcdef"[addr[i] / 16], 1);
		write(1, &"0123456789abcdef"[addr[i] % 16], 1);
		i++;
	}
	while (i < 16)
	{
		if (i % 2 == 0)
			write(1, " ", 1);
		write(1, "  ", 2);
		i++;
	}
	write(1, " ", 1);
}

void	total_p(unsigned char *addr, unsigned int size)
{
	unsigned char	i;

	i = 0;
	while (i < 16 && i < size)
	{
		if (addr[i] >= 32 && addr[i] <= 126)
			write(1, &addr[i], 1);
		else
			write(1, ".", 1);
		i++;
	}
	write(1, "\n", 1);
}

void	*ft_print_memory(void *addr, unsigned int size)
{
	unsigned int	i;

	i = 0;
	while (i < size)
	{
		address((unsigned long)addr + i);
		change_hexde((unsigned char *)addr + i, size - i);
		total_p((unsigned char *)addr + i, size - i);
		i += 16;
	}
	return (addr);
}

세 가지 파트로 나뉜다.

  1. 첫 문자 → 16진수
  2. 전체 문자 → 16진수
  3. 전체 문자 출력

입력 받기

  • 16개씩 끊어야 한다.

첫 문자 → 16진수

  • 포인터를 활용