๐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์ ๋ฐํํ๋ค.
'๐ฐ42์์ธ > libft' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[libft] ft_memset, ft_bzero, ft_memchr ๊ตฌํ (0) | 2022.02.15 |
---|---|
[libft] ft_strncmp, ft_memcmp ๊ตฌํ (0) | 2022.02.14 |
[42์์ธ] libft ์ ๋ฆฌ : ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ง๋ค๊ธฐ ์ํ Makefile (0) | 2022.02.11 |
[42์์ธ] libft ์ ๋ฆฌ : ft_strlen, ft_strlcpy, ft_strlcat ๊ตฌํ (0) | 2022.02.07 |
[42์์ธ] libft ์ ๋ฆฌ : ft_toupper, ft_tolower ๊ตฌํ (0) | 2022.01.14 |