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

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



13.5 Using the C String Library
13.6 String Idioms
13.7 Array of Strings



๐Ÿ“ Using the C String Library

C์—์„œ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์‚ฌ๋ฅผ ํ•˜๊ฑฐ๋‚˜ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค.

C์—์„œ๋Š” ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋‹ด๊ธด ํ—ค๋”๊ฐ€ ์กด์žฌ

#include <string.h>



๐Ÿ“ The strcpy (String Copy) Function

๐Ÿ”Ž strcpy ํ•จ์ˆ˜

char* strcpy(char* destination, const char* source);

source ๋ฌธ์ž์—ด์„ destination ๋ฌธ์ž์—ด์— ๋ณต์‚ฌํ•ด์ค€๋‹ค.
(์ •ํ™•ํžˆ๋Š” source๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์„ destination์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฐ์—ด์— ๋ณต์‚ฌ)

strcpy๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

str2 = "abcd"; (x)

strcpy(str2, "abcd"); (o)

strcpy(str1, str2); (o)

โ— strcpy(str1, str2)๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ str2๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์ด str1์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์— ๋งž๋Š”์ง€ ํŒ๋‹จ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

=> ๋ฌธ์ž์—ด์„ ๋ณต์‚ฌํ•  ๋• strncpy ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๊ฒŒ ๋Š๋ฆฌ๋”๋ผ๋„ ์•ˆ์ „ํ•˜๋‹ค.

๐Ÿ”Ž strncpy ํ•จ์ˆ˜ ์‚ฌ์šฉ ์˜ˆ์‹œ

strncpy(str1, str2, sizeof(str1));

์ด ๊ฒฝ์šฐ๋„ str2๊ฐ€ ๋” ํฌ๋ฉด str1์ด null character๋กœ ๋๋‚˜์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋” ์•ˆ์ „ํ•˜๋‹ค.

strncpy(str1, str2, sizeof(str1)-1);
str1[sizeof(str1)-1] = '\0';



๐Ÿ“ The strlen (String Length) Function

๐Ÿ”Ž strlen ํ•จ์ˆ˜

size_t strlen(const char* str); 

(size_t๋Š” unsingned integer ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ˜• ์ด๋ฆ„)

strlen์€ ๋ฌธ์ž์—ด str์˜ '๊ธธ์ด'๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋ฐฐ์—ด์ด ์ž…๋ ฅ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ ๋ฐฐ์—ด ์ž์ฒด์˜ ๊ธธ์ด๊ฐ€ ์•„๋‹Œ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.




๐Ÿ“ The strcat (String Concatenation) Function

๐Ÿ”Ž strcat ํ•จ์ˆ˜

char* strcat(char* destination, const char* source); 

strcat์€ ๋ฌธ์ž์—ด source์˜ ๋‚ด์šฉ์„ ๋ฌธ์ž์—ด destination ๋์— ์ด์–ด ๋ถ™์ธ๋‹ค.

๐Ÿ”Ž strcat ํ•จ์ˆ˜ ์‚ฌ์šฉ ์˜ˆ์‹œ

strcpy(str1, "abc");
strcat(str1, "def"); // str1 : "abcdef"

๐Ÿ”Ž strncat ํ•จ์ˆ˜

char* strncat(str1, str2, sizeof(str1) - strlen(str1) -1);

strncat์€ ๋ณต์‚ฌํ•  ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜์— ์ œํ•œ์„ ๋‘๋ฏ€๋กœ ๋” ๋Š๋ฆฌ์ง€๋งŒ ์•ˆ์ „ํ•˜๋‹ค.

strncat์€ str1 ๋์— null character๋ฅผ ๋„ฃ์–ด์ฃผ๋Š”๋ฐ ์ด๋Š” ์„ธ๋ฒˆ์งธ ์ž…๋ ฅ๋ณ€์ˆ˜์— ํฌํ•จ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ฏ€๋กœ ์„ธ๋ฒˆ์งธ ์ž…๋ ฅ๋ณ€์ˆ˜๋Š” ์ด๋ฅผ ๋„ฃ์–ด์ค„ ๊ณต๊ฐ„์„ ํ™•๋ณดํ•ด๋†“์•„์•ผ ํ•œ๋‹ค.



๐Ÿ“ The strcmp (String Comparison) Function

๐Ÿ”Ž strcmp ํ•จ์ˆ˜

int strcmp(const char* str1, const char* str2);

strcmp๋Š” str1๊ณผ str2๋ฅผ ๋น„๊ตํ•ด str1์ด ๋” ์ž‘์œผ๋ฉด 0๋ณด๋‹ค ์ž‘์€ ๊ฐ’, ๊ฐ™์œผ๋ฉด 0, ํฌ๋ฉด 0๋ณด๋‹ค ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋น„๊ต๋ฅผ ํ•  ๋•Œ๋Š” ์‚ฌ์ „์‹ ์ˆœ์„œ๋กœ ๋น„๊ต๋ฅผ ํ•œ๋‹ค. (๋ฌธ์ž๋“ค์ด ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค)

๐Ÿ”Ž strcmp ํ•จ์ˆ˜ ์‚ฌ์šฉ ์˜ˆ์‹œ

if (strcmp(str1, str2) <0) { // st1 < str2 ?
}

๐Ÿ”‘ ASCII ๋ฌธ์ž์—ด ์ง‘ํ•ฉ์˜ ๋ช‡๊ฐ€์ง€ ์ค‘์š”ํ•œ ํŠน์„ฑ๋“ค

  • A~Z, a~z๋ผ๋Š” ์—ฐ์†๋œ ๋ฌธ์ž๋“ค์€ ์—ฐ์†๋œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘๋‹ค.
  • ์ˆซ์ž๋Š” ๋ฌธ์ž๋ณด๋‹ค ์ž‘๋‹ค
  • ๋„์–ด์“ฐ๊ธฐ๋Š” ๋ชจ๋“  ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋ณด๋‹ค ์ž‘๋‹ค.



๐Ÿ”” ์ค‘๊ฐ„ ์š”์•ฝ (13.5 C Library)

  • strcpy ( ๋ฌธ์ž์—ด ๋ณต์‚ฌ ํ•จ์ˆ˜ ) : source๋ฅผ destination์— ๋ณต์‚ฌ
char* strcpy(char* destination, const char* source);
  • strlen ( ๋ฌธ์ž์—ด ํฌ๊ธฐ ํ•จ์ˆ˜ ) : str์˜ ๊ธธ์ด ๋ฐ˜ํ™˜
size_t strlen(const char* str);
  • strcat ( ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ํ•จ์ˆ˜ ) : source์˜ ๋‚ด์šฉ์„ destination ๋์— ์ด์–ด๋ถ™์ด๊ณ , destination ๋ฐ˜ํ™˜
char* strcat(char* destination, const char* source);
  • strcmp ( ๋ฌธ์ž์—ด ๋น„๊ต ํ•จ์ˆ˜ ) : str1์ด str2๋ณด๋‹ค ํฌ๋ฉด 0๋ณด๋‹ค ํฐ ๊ฐ’, ๊ฐ™์œผ๋ฉด 0, ์ž‘์œผ๋ฉด 0๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๋ฐ˜ํ™˜
int strcmp(const char* str1, const char* str2);



๐Ÿ“ Searching for the End of a String

๐Ÿ”Ž strlen ์ง์ ‘ ๊ตฌํ˜„ - (1)

size_t strlen(const char* str)
{
	size_t size;

	for (size = 0; *str != '\0'; ++str) {
		++size;
	}

	return size;
}

๐Ÿ”Ž strlen ์ง์ ‘ ๊ตฌํ˜„ - (2) ์ข€ ๋” ์••์ถ•

null character์˜ ์ •์ˆ˜๊ฐ’์€ 0์ด๋ฏ€๋กœ str != '\0'์€ str != 0๊ณผ ๊ฐ™๊ณ , ์ด๋ฅผ ํŒ๋ณ„ํ•ด์ฃผ๋Š”๊ฑด *str์„ ํŒ๋ณ„ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

size_t strlen(const char* str)
{
	size_t size = 0;

	for (; *str++;) {
		++size;
	}

	return size;
}

๐Ÿ”Ž strlen ์ง์ ‘ ๊ตฌํ˜„ - (3) while๋ฌธ ์ด์šฉ

size_t strlen(const char* str)
{
	size_t size = 0;

	while(*str++) {
		++size;
	}

	return size;
}

์œ„ ๋ฐฉ๋ฒ•๋“ค์€ ์„ฑ๋Šฅ์€ ๋™์ผ

๐Ÿ”Ž strlen ์ง์ ‘ ๊ตฌํ˜„ - (4) ์„ฑ๋Šฅ ํ–ฅ์ƒ (์ผ๋ถ€ ์ปดํŒŒ์ผ๋Ÿฌ)

size_t strlen(const char* str)
{
	const char* orig_str = str;

	while (*str) {
		++str;
	}

	return str - orig_str; 
}

size๋ฅผ ์„ ์–ธ ๋ฐ ์ฆ๊ฐ€ x

whlie ๋ฌธ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

while (*str++) {
}

โ— ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€๋Š” ์•Š์œผ๋‹ˆ ์›ฌ๋งŒํ•˜๋ฉด for๋ฌธ์„ ์ด์šฉํ•ด '\0'์„ ์ฐพ๊ณ  ์žˆ์Œ์„ ๋“œ๋Ÿฌ๋‚ด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.



๐Ÿ“ Copying a String

๐Ÿ”Ž strcat ์ง์ ‘ ๊ตฌํ˜„ - (1)

char* strcat(char* destination, const char* source)
{
	char* ptr = destination;
	while (*ptr != '\0') {
		++ptr;
	} // ptr์ด destination์˜ ๋์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋งŒ๋“ค๊ธฐ

	while (*source != '\0') {
		*ptr = *source;
		++ptr;
		++source; 
	} // ptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์— source ํ•˜๋‚˜์”ฉ ๋ณต์‚ฌํ•ด์„œ ๋„ฃ๊ธฐ

	*ptr = '\0';
	return destination;
}

๐Ÿ”Ž strcat ์ง์ ‘ ๊ตฌํ˜„ - (2) ๊ฐ„์†Œํ™”


char* strcat(char* destination, const char* source)
{
	char* ptr = destination;
	while (*ptr) {
		++ptr;
	} 

	while (*ptr++ = *source++) {
	} 

	*ptr = '\0';
	return destination;
}

๊ฐ„์†Œํ™”๋œ strcat์˜ ํ•ต์‹ฌ์ด ๋ฌธ์ž์—ด ๋ณต์‚ฌ ๊ด€์šฉ๊ตฌ

while (*ptr++ = *source++) {
}

source๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž๋ฅผ ptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์— ๋ณต์‚ฌํ•ด์ค€ ํ›„, ++์—ฐ์‚ฐ์ž์— ์˜ํ•ด ์ฆ๊ฐ€๋œ๋‹ค.
while๋ฌธ์€ ๋ณต์‚ฌ๋œ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ํŒ๋‹จ์„ ํ•˜๋ฏ€๋กœ ๋ชจ๋“  ๋ฌธ์ž๋Š” ๊ฐ’์ด ์ฐธ์ด ๋˜์–ด ๋„ ๋ฌธ์ž๋ฅผ ๋ณต์‚ฌํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋ฃจํ”„๋Š” ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ ๋ฃจํ”„๋Š” ๋„ ๋ฌธ์ž ํ• ๋‹น ์ดํ›„์— ์ข…๋ฃŒ๊ฐ€ ๋˜๋ฏ€๋กœ ๋”ฐ๋กœ ๋„ ๋ฌธ์ž๋ฅผ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.



๐Ÿ“ Arrays of String

char planets[][8] = { "Mercury", "Venus", "Earth",
		"Mars", "Jupiter", "Saturn", 
		"Uranus", "Neptune", "Pluto" };

์ด๋Ÿฐ์‹์œผ๋กœ 2์ฐจ์› ๋ฐฐ์—ด์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ๊ฒฝ์šฐ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋นˆ ๊ณต๊ฐ„์—๋Š” ๋„ ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ž‰์—ฌ ๊ณต๊ฐ„์ด ์ƒ๋‹นํžˆ ๋ฐœ์ƒํ•œ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ๊ฐ ์—ด๋“ค์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์ธ ๋ˆ„๋”๊ธฐ ๋ฐฐ์—ด(ragged array)๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
์›์†Œ๋“ค์ด ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ฉด ์ด๋Ÿฌํ•œ ๋ฐฐ์—ดํ˜•์„ ํ‰๋‚ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

planets๋ฅผ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋“ค์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

char* planets[] = { "Mercury", "Venus", "Earth",
		"Mars", "Jupiter", "Saturn",
		"Uranus", "Neptune", "Pluto" };

์ด๋Ÿฐ์‹์œผ๋กœ ํ‘œํ˜„ํ•  ๊ฒฝ์šฐ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด์ง„๋‹ค.

planets์˜ ๊ฐ ์›์†Œ๋“ค์€ ๋„ ๋ฌธ์ž๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด์˜ ํฌ์ธํ„ฐ๋“ค์ด๋ฉฐ planets์˜ ๋ฐฐ์—ด์— ํฌ์ธํ„ฐ๋ฅผ ์œ„ํ•œ ๊ณต๊ฐ„์ด ํ• ๋‹น๋œ๋‹ค.

ํ–‰์„ฑ ์ด๋ฆ„์— ์ ‘๊ทผํ•˜๋ ค๋ฉด planets ๋ฐฐ์—ด์— ์ฒจ์ž๋ฅผ ์จ์ฃผ๋ฉด ๋œ๋‹ค.




๐Ÿ“ Command-Line Arguments

๋ช…๋ นํ–‰ ์ •๋ณด๋Š” ์šด์˜์ฒด์ œ ๋ช…๋ น์–ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜(command-line arguments(program parameter))์— ์ ‘๊ทผํ•˜๋ ค๋ฉด mainํ•จ์ˆ˜์— ๋ณดํ†ต argc์™€ argv๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ–๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ”Ž ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๊ธฐ

int main(int argc, char* argv[])
{
...
}

argc (argument count) : ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜(ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„๋„ ํฌํ•จ)
argv (argument vector) : ๋ฌธ์ž์—ด๋กœ ์ €์žฅ๋˜๋Š” ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋“ค์˜ ๋ฐฐ์—ด

argv[0]์€ ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„์„ ๊ฐ€๋ฆฌํ‚ค๊ณ , argv[1]๋ถ€ํ„ฐ arvg[argc-1]๊นŒ์ง€๋Š” ๋‚˜๋จธ์ง€ ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜๋“ค์„ ์˜๋ฏธํ•œ๋‹ค.

argv[argc]๋Š” null pointer์ด๋‹ค.

๐Ÿ”Ž ๋ช…๋ นํ–‰ ์ž…๋ ฅ๋ณ€์ˆ˜ ์˜ˆ์‹œ

ls -l remind.c

argc = 3
argv[0] = program name
argv[1] = "-l"
argv[2] = "remind.c"
argv[3] = null pointer



์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ