๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿฐ42์„œ์šธ/libft

[42์„œ์šธ] libft ์ •๋ฆฌ : ft_strchr, ft_strrchr๊ตฌํ˜„

๐Ÿ“Œstrchr

โœ”๏ธ ํ•จ์ˆ˜ ํ”„๋กœํ† ํƒ€์ž…

char	*strchr(const char *s, int c);

โœ”๏ธ ํ•จ์ˆ˜์˜ ์—ญํ•  : ๋ฌธ์ž์—ด์—์„œ ์ฒ˜์Œ ๋ฐœ๊ฒฌ๋˜๋Š” c๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โœ”๏ธํ•จ์ˆ˜ ๋ฐ˜ํ™˜๊ฐ’ 

โœ๏ธ c๊ฐ€ ๋ฌธ์ž์—ด ๋‚ด์— ์กด์žฌํ•  ๊ฒฝ์šฐ, c๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ

โœ๏ธ c๊ฐ€ ๋ฌธ์ž์—ด ๋‚ด์— ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, NULL

  

โœ”๏ธ๊ตฌํ˜„

char	*ft_strchr(const char *s, int c)
{
	int		i;
	char	*p;

	i = 0;
	p = (char *)s;
	while (p[i] != 0)
	{
		if ((char)c == p[i])
			return (p + i);
		i++;
	}
	if (c == 0)
		return (p + i);
	else
		return (NULL);
}

๐Ÿ“Œstrrchr

โœ”๏ธ ํ•จ์ˆ˜ ํ”„๋กœํ† ํƒ€์ž…

 char	*strrchr(const char *s, int c);

โœ”๏ธ ํ•จ์ˆ˜์˜ ์—ญํ•  : ๋ฌธ์ž์—ด์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐœ๊ฒฌ๋˜๋Š” c๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โœ”๏ธํ•จ์ˆ˜ ๋ฐ˜ํ™˜๊ฐ’ 

โœ๏ธ c๊ฐ€ ๋ฌธ์ž์—ด ๋‚ด์— ์กด์žฌํ•  ๊ฒฝ์šฐ, c๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ

โœ๏ธ c๊ฐ€ ๋ฌธ์ž์—ด ๋‚ด์— ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, NULL

  

โœ”๏ธ๊ตฌํ˜„

char	*ft_strrchr(const char *s, int c)
{
	int		len;
	char	*p;

	p = (char *)s;
	len = ft_strlen(s);
	while (len >= 0)
	{
		if ((char)c == p[len])
			return (p + len);
		len--;
	}
	return (NULL);
}

โœ”๏ธ man page์— ๋‚˜์˜จ ์„ค๋ช…!

๐Ÿ‘€DESCRIPTION
The strchr() function locates the first occurrence of c (converted to a char) in the string pointed to by s.  The terminating null character is considered to be part of the string; therefore if c is ‘\0’, the functions locate the terminating ‘\0’.
    The strrchr() function is identical to strchr(), except it locates the last occurrence of c.
โžฟํ•ด์„ :
strchr() ํ•จ์ˆ˜๋Š” s๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์—์„œ ์ฒ˜์Œ ๋ฐœ๊ฒฌ๋˜๋Š” c(char๋กœ ๋ณ€ํ™˜๋จ)๋ฅผ ์ฐพ๋Š”๋‹ค. ์ข…๋ฃŒํ•˜๋Š” ๋„๋ฌธ์ž ๋˜ํ•œ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋กœ ๋ณธ๋‹ค; ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋งŒ์•ฝ c๊ฐ€ '\0'์ด๋ฉด ํ•จ์ˆ˜๋Š” ์ข…๋ฃŒํ•˜๋Š” '\0'์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
strrchr()ํ•จ์ˆ˜๋Š” ๋งˆ์ง€๋ง‰ c์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ ๋Š” strchr()๊ณผ ๋™์ผํ•˜๋‹ค.
๐Ÿ‘€RETURN VALUES

     The functions strchr() and strrchr() return a pointer to the located character, or NULL if the character does not appear in the string.
โžฟํ•ด์„ :
strchr() ํ•จ์ˆ˜์™€ strrchr() ํ•จ์ˆ˜๋Š” ์œ„์น˜ํ•œ ๋ฌธ์ž์˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ˜น์€ ๋ฌธ์ž์—ด ๋‚ด์— ๋ฌธ์ž๊ฐ€ ์—†์„ ์‹œ์—๋Š” NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.