๐Ÿ“˜13์žฅ-(1) Strings | Study

ํŒŒ์ธยท2022๋…„ 1์›” 16์ผ
0

C

๋ชฉ๋ก ๋ณด๊ธฐ
1/9
post-thumbnail
post-custom-banner

C Programming, A Modern Approach - K.N.KING์œผ๋กœ C์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
๋ฒˆ์—ญ๋ณธ https://wikidocs.net/book/2494 ์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


13.1 String Literals
13.2 String Variables
13.3 Reading and Writing Strings
13.4 Accessing the Characters in a String


๐Ÿ“ How String Literals Are Stored

C์—์„œ๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด(String Literals)์„ character๋“ค์˜ array๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
๋ฌธ์ž์—ด์˜ ๋์ž„์„ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์œผ๋กœ null character์„ ๋„ฃ์–ด์ฃผ๋ฉฐ ์ด๋Š” \0์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ์ด null character๋กœ ์ธํ•ด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” n ๊ธธ์ด์˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด n+1 byte๋ฅผ ํ• ๋‹นํ•ด์ค€๋‹ค.

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด ๋ฐฐ์—ด์œผ๋กœ์„œ ์ €์žฅ์ด ๋˜๋ฏ€๋กœ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด๋ฅผ char* ํ˜•์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

printf("abc");

์ด ๊ฒฝ์šฐ "abc"์˜ ์ฃผ์†Œ ์ฆ‰, a ๋ฌธ์ž๊ฐ€ ์ €์žฅ๋œ ๊ณณ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์ค€๋‹ค. printf๋Š” ์ฒซ๋ฒˆ์งธ ์ž…๋ ฅ๋ณ€์ˆ˜์— ํฌ์ธํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.

๋ฌธ์ž์—ด ๋˜ํ•œ ์ฒจ์ž([])๊ฐ€ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

char ch;
ch = "abc"[1];

๐Ÿ“ Initializing a String Variable

char date1[9] = "June 14";

์œ„์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋ฌธ์ž์—ด ํฌ๊ธฐ+1๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ฌธ์ž ๋ฐฐ์—ด์˜ ๋‚˜๋จธ์ง€ ์›์†Œ๋“ค์€ \0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค.

๋ฌธ์ž์—ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ๊ณ„์‚ฐํ•ด์ค€๋‹ค.

char date1[] = "June 14";

์ด ๊ฒฝ์šฐ ์•Œ์•„์„œ 8๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ๊ณต๊ฐ„์ด ํ• ๋‹น๋œ๋‹ค.


๐Ÿ“ Character Arrays versus Character Pointers

1) date๋ฅผ '๋ฐฐ์—ด'๋กœ ์„ ์–ธํ•˜๊ธฐ

char date[] = "June 14";
  • ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์—์„œ ์›์†Œ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋“ฏ์ด date์— ์ €์žฅ๋œ ๋ฌธ์ž๋“ค์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • date๋Š” ๋ฐฐ์—ด์˜ ์ด๋ฆ„
    2) date๋ฅผ 'ํฌ์ธํ„ฐ'๋กœ ์„ ์–ธํ•˜๊ธฐ
char* date = "June 14";
  • ํฌ์ธํ„ฐ๊ฐ€ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.
  • date๋Š” ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋Š” '๋ณ€์ˆ˜'

๊ณตํ†ต์  ) ๋ฌธ์ž ๋ฐฐ์—ด์ด๋‚˜ ๋ฌธ์ž ํฌ์ธํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ๋ณ€์ˆ˜๋กœ ๋‘ date๋‹ค ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ „์— ๋ฐ˜๋“œ์‹œ ๋ฌธ์ž ๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

char str[STR_LEN+1];
char* p;

p = str;

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด p๊ฐ€ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜(str์˜ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ๋˜๊ณ , p๋ฅผ ๋ฌธ์ž์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ“ Reading Strings Using printf and puts

printf ์—์„œ %s๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์จ์ค„ ์ˆ˜ ์žˆ๋‹ค.

char str[] = "Are you having fun yet?";
	printf("%s", str);
// ๊ฒฐ๊ณผ
Are you having fun yet?

๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜ ์ฝ๋‹ค๊ฐ€ \0(null character)์„ ๋งŒ๋‚˜๋ฉด ์ค‘๋‹จํ•œ๋‹ค.
์ผ๋ถ€๋ถ„๋งŒ ์ถœ๋ ฅํ•˜๋ ค๋ฉด %.ps ๋ฅผ ์“ฐ๋ฉด ๋œ๋‹ค.
(p : ์ถœ๋ ฅํ•  ๋ฌธ์ž ๊ฐฏ์ˆ˜)
ํŠน์ • ๋ฒ”์œ„๋‚ด์—์„œ ์ถœ๋ ฅํ•˜๋ ค๋ฉด %ms๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
(m : ํŠน์ • ๋ฒ”์œ„)
๋ฌธ์ž์—ด์ด m๋ณด๋‹ค ์ ์€ ๋ฌธ์ž๋ฅผ ๊ฐ–๋Š”๋‹ค๋ฉด ๋ฒ”์œ„ ๋‚ด ์šฐ์ธก ์ •๋ ฌ

C์—์„œ๋Š” puts ๋ฅผ ์ด์šฉํ•ด์„œ ์ถœ๋ ฅ์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. puts ๋Š” ํ•œ ๊ฐ€์ง€ ์ž…๋ ฅ๋ณ€์ˆ˜๋งŒ ๋ฐ›๋Š”๋‹ค.

puts(str);

๐Ÿ“ Reading Strings Using scanf and gets

scanf ์—์„œ %s๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.

scanf("%s", str);

str์€ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋ฉด ํฌ์ธํ„ฐ ์ทจ๊ธ‰์„ ๋ฐ›์œผ๋ฏ€๋กœ &์„ ๋ถ™์—ฌ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

scanf๋Š” ๋ฌธ์ž์—ด์˜ ๋์— null character์„ ์ €์žฅํ•ด์ค€๋‹ค.
scanf๋Š” ๊ณต๋ž€๋ฌธ์ž๋ฅผ ์ฝ์„ ์ˆ˜ ์—†๋‹ค
-> "gets"๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐ

gets๋Š” ๊ณต๋ž€๋ฌธ์ž๋ฅผ ์ƒ๋žตํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ฐพ์„ ๋•Œ ๊นŒ์ง€ ๊ณ„์† ์ฝ๋Š”๋‹ค. ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ฝ์œผ๋ฉด ๋ฐฐ์—ด์— ๋„ฃ์ง€์•Š๊ณ , ๊ทธ ์ž๋ฆฌ์— null character์„ ๋„ฃ๋Š”๋‹ค.

** scanf์™€ gets๋Š” ๋ฐฐ์—ด์— ๋ฌธ์ž๋ฅผ ์ฝ์„ ๋•Œ ๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐพ๋Š”์ง€ ํ™•์ธ ๋ถˆ๊ฐ€ํ•˜๋‹ค. -> %s๋Œ€์‹  %ns, gets๋Œ€์‹  fgets๋ฅผ ์ด์šฉํ•˜๋ฉด ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
(n : ์ €์žฅํ•  ์ตœ๋Œ€ ๋ฌธ์ž ๊ฐœ์ˆ˜)


๐Ÿ“ Reading Strings Character by Character

๋ณดํ†ต์€ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ํ•˜๋‚˜๋งˆ๋‹ค ์ฝ์–ด์ฃผ๋Š” ์ž…๋ ฅ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘๋งŒ๋“ ๋‹ค. (scanf, gets๋Š” ์•ˆ์ „ํ•˜์ง€ x)

๐Ÿ”Ž๋ฌธ์ž์—ด ์ฝ๋Š” ํ•จ์ˆ˜ read_line
1. ๊ณต๋ž€ ๋ฌธ์ž ๋ฌด์‹œx
2. ๊ฐœํ–‰๋ฌธ์ž(\n)์—์„œ ์ฝ๊ธฐ ๋ฉˆ์ถค
3. ์ถ”๊ฐ€์ ์ธ ๋ฌธ์ž๋Š” ๋ฒ„๋ฆฌ๊ธฐ

int read_line(char str[], int n) 
{
	int ch; // getchar()์€ int๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ 
	int i = 0;

	while ((ch = getchar()) != '\n') { // ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€
		if (i < n) { // n๊ฐœ ๊นŒ์ง€๋งŒ ๋ฐ›๊ธฐ
			str[i++] = ch; // ๋ฌธ์ž ๋ฐฐ์—ด์— ์ €์žฅ
		}
	}
	str[i] = '\0'; // ๋งˆ์ง€๋ง‰๊บผ๋Š” null character
	return i; // ๋ฌธ์ž์—ด ํฌ๊ธฐ 
}

๐Ÿ“ Accessing the Characters in a String

๋ฌธ์ž์—ด์€ ๋ฐฐ์—ด๋กœ ์ €์žฅ๋˜๋ฏ€๋กœ ์ฒจ์ž [] ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ”Ž๋ฐฐ์—ด ์ฒจ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋‚ด ๋„์–ด์“ฐ๊ธฐ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ํ•จ์ˆ˜

int count_spaces(const char s[])
{
	int count = 0;
	int i;

	for (i = 0; s[i] != '\0'; ++i) { // null character๋งŒ๋‚ ๋•Œ๊นŒ์ง€ (๋ฌธ์ž์—ด ๋๊นŒ์ง€)
		if (s[i] == ' ') { // ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๋งŒ๋‚˜๋ฉด 
			++count; // count์— 1๋”ํ•˜๊ธฐ
		}
	}
	return count;
}

์‹ค์ œ๋กœ๋Š” ๋ฐฐ์—ด ์ฒจ์ž๋Œ€์‹  ํฌ์ธํ„ฐ ์‚ฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑ

๐Ÿ”Žํฌ์ธํ„ฐ ์‚ฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋‚ด ๋„์–ด์“ฐ๊ธฐ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ํ•จ์ˆ˜


int count_spaces(const char *s)
{
	int count = 0;

	for (; *s != '\0'; ++s) { // s๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒŒ null character๊ฐ€ ์•„๋‹๋•Œ๊นŒ์ง€
		if (*s == ' ') {
			++count;
		}
	}
	return count;
}

const๋Š” s๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์„ ๋ณดํ˜ธํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฏ€๋กœ s๋Š” ์ˆ˜์ •๊ฐ€๋Šฅํ•˜๋‹ค
(s๋Š” ์• ์ดˆ์— ํฌ์ธํ„ฐ์˜ ๋ณต์‚ฌ๋ณธ)

โ— ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž์— ์ ‘๊ทผํ•  ๋•Œ๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ๋‚˜ ์ฒจ์ž, ํฌ์ธํ„ฐ ๋ญ˜ ์“ฐ๋“  ์ƒ๊ด€์—†๋‹ค.

profile
๊ณต๋ถ€์ •๋ฆฌ์šฉ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€