Do You Coding?

[library] strchr ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[library] strchr ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
       strchr - locate character in string
: ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž๋ฅผ ์ฐพ๊ธฐ


SYNOPSIS
       #include <string.h>
       char *strchr(const char *s, int c);

DESCRIPTION
       The  strchr()  function  returns  a  pointer to the first occurrence of the character c in the
       string s.
       Here "character" means "byte"; these functions do not work with wide or multibyte characters.

: strchr() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž c์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐœ์ƒ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ๋ฌธ์ž(character)๋Š” ๋ฐ”์ดํŠธ(byte)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์€ ๋„“์€ ๋ฌธ์ž๋‚˜ ์—ฌ๋Ÿฌ ๋ฐ”์ดํŠธ ๋ฌธ์ž์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.


RETURN VALUE
       The strchr() and strrchr() functions return a pointer to the matched character or NULL if  the
       character  is  not found.  The terminating null byte is considered part of the string, so that
       if c is specified as '\0', these functions return a pointer to the terminator.

: strchr() ๋ฐ strrchr() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž๊ฐ€ ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์œผ๋ฉด ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž ๋˜๋Š” NULL๋กœ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ NULL ๋ฐ”์ดํŠธ๋Š” ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋กœ ๊ฐ„์ฃผ๋˜๋ฏ€๋กœ c๊ฐ€ '\0'์œผ๋กœ ์ง€์ •๋˜๋ฉด ์ด ํ•จ์ˆ˜๋Š” ์ข…๊ฒฐ ๋ถ€๋ถ„์œผ๋กœ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ“Œ ์ž‘์„ฑ ์ฝ”๋“œ

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

	i = 0;
	while (s[i] != '\0')
	{
		if (s[i] == c)
			return ((char *)s + i);
		i ++;
	}
	if (c == '\0')
		return ((char *)s + i);
	return (0);
}

 

๐Ÿ“Œ ์ฝ”๋“œ ๋ฆฌ๋ทฐ

strchr์€ int c ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๊ฐ’๊ณผ ๋™์ผํ•œ ๋ฌธ์ž๋ฅผ ์ฐพ์œผ๋ฉด ํ•ด๋‹น ์œ„์น˜์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ธ๋ฑ์Šค์ธ i๋ฅผ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ ์ด์šฉํ•ด s[i]๊ฐ€ \0์ด ์•„๋‹Œ ๋™์•ˆ s[i]๊ฐ€ c์™€ ๊ฐ™์€์ง€ if๋ฌธ์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋งŒ์•ฝ c๋ฅผ ์ฐพ๊ฒŒ ๋˜๋ฉด,

ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, s๋Š” ๋ฌธ์ž์—ด์˜ ์ฒซ๋ฒˆ์งธ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ถ”๊ฐ€๋˜์–ด์ง„ ์ธ๋ฑ์Šค์ธ i๋ฅผ ๋”ํ•˜์—ฌ returnํ•œ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ๋ชจ๋‘ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ์•˜๋Š”๋ฐ๋„ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋ฉด, 0์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๊ณ ,

๋งŒ์•ฝ, c ๊ฐ€ \0์ด๋ผ๋ฉด ์˜ˆ์™ธ์ ์œผ๋กœ ๋ฌธ์ž์—ด ๋งˆ์ง€๋ง‰ ์œ„์น˜์˜ ๋‹ค์Œ์„ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์ด \0์ด ์žˆ๋Š” ์ข…๊ฒฐ ์œ„์น˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋Ÿฌ๋ ค๋ฉด while๋ฌธ์ด ๋‹ค ๋Œ๊ณ ๋‚˜์„œ s์— ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ ์ธ๋ฑ์Šค i๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด \0๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ํฌ์ธํ„ฐ๊ฐ€

s + i ์— ์žˆ๊ณ  ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ถ”๊ฐ€์ ์œผ๋กœ return ๊ฐ’๋“ค์— (char *)์„ ๋ช…์‹œ์  ์บ์ŠคํŒ…ํ•ด์ฃผ์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค.

์บ์ŠคํŒ… ํ•˜์ง€ ์•Š์œผ๋ฉด, return discards ‘const’ qualifier from pointer target type ๋ผ๋Š” warning ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ, 

์ด๋Š” strchr ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด (char *) ์ด๋ฏ€๋กœ, const๊ฐ€ ์‚ฌ๋ผ์ง„ ๊ฒƒ์— ๋Œ€ํ•œ ๊ฒฝ๊ณ ๋ฅผ ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

์บ์ŠคํŒ…ํ•˜์ง€ ์•Š์•„๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์ง€๋งŒ, const๋กœ ๋œ ๊ฒƒ์„ ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋Š” return ์ž๋ฃŒํ˜•์— ๋งž์ถฐ ์บ์ŠคํŒ…ํ•œ ๋’ค ๋ฐ˜ํ™˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.