This chapter presents strings, a special type of array of characters. We introduce string constants and variables and examine the close relationship strings have with pointers. We present the standard library functions for manipulating strings and provide array-subscripting and pointer-indexing implementations of several of the more commonly used ones. We also present an implementation of a useful function for reading an input line into a string and rework several of our earlier programs to use it. We conclude with a case study: a program that strips consecutive duplicate lines from its input.
Jump to: [Previous Chapter | Next Chapter]
char str[9] = {'A', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0'};
char str[] = "A string";
char *str = "A string";
1) Character array·Î ó¸®
int FindFirstVowel(char str[]) { int i; for (i = 0; str[i] != '\0'; i++) if (IsVowel(str[i])) return i; return (-1); }
2) Pointer to character·Î ó¸®
int FindFirstVowel(char *str) { char *cp; for (cp = str; *cp != '\0'; cp++) if (IsVowel(*cp)) return (cp - str); return (-1); }
¹æ¹ý 1)
void strcpy(char *s2, char *s1) { int i = 0; while ((s2[i] = s1[i]) != '\0') i++; }
¹æ¹ý 2) while ((*s2 = *s1) != '\0') { s2++; s1++; }
¹æ¹ý 3) while ((*s2++ = *s1++) != '\0') ;
¹æ¹ý 4) while (*s2++ = *s1++) ;
1) "hello"¿Í °°Àº string »ó¼ö´Â ¹®ÀÚ¿ÀÌ ÀúÀåµÈ ¸Þ¸ð¸® ÁּҷΠ󸮵ȴÙ.
2) StringÀÇ arrayÇü »ç¿ë°ú pointerÇü »ç¿ëÀÇ Â÷À̸¦ ÀνÄÇÑ´Ù
3) String copy ½Ã target stringÀº original string¿¡ Æ÷ÇÔµÈ ¸ðµç ¹®ÀÚµéÀ» ÀúÀåÇϱâ ÃæºÐÇÑ ¸Þ¸ð¸®¸¦ È®º¸ÇÏ°í ÀÖ¾î¾ß ÇÑ´Ù.
4) String return ½Ã local º¯¼öó·³ ¼Ò¸êµÇ´Â ¸Þ¸ð¸®¸¦ »ç¿ëÇÏÁö ¸»¾Æ¾ß ÇÑ´Ù.
string CharToString(char ch) { char str[2]; str[0] = ch; str[1] = '\0'; return (str); }
À§ functionÀº ¹®ÀÚ¿ÀÌ ÀúÀåµÈ ¸Þ¸ð¸® ÁÖ¼Ò¸¸À» returnÇÑ´Ù.
½ÇÁ¦ ¹®ÀÚ¿ÀÌ ÀúÀåµÈ ¸Þ¸ð¸®´Â function
½ÇÇàÀÌ ³¡³ª¸é ÀÚµ¿ ¼Ò¸êµÇ¾î Àç»ç¿ëµÈ´Ù.
±×·¯¹Ç·Î ÇÁ·Î±×·¥ ½ÇÇà Áß ³»¿ëÀÌ ¹Ù²ð ¼ö ÀÖ´Ù.
"string.h"¿¡ prototypeÀÌ Á¤ÀǵǾî ÀÖ´Ù.
Function | ±â´É |
strcpy(dst, src) | string src¸¦ dst·Î º¹»ç |
strncpy(dst, src, n) | ÃÖ´ë n ¹®ÀÚ¸¦ src¿¡¼ dst·Î º¹»ç |
strcat(dst, src) | string dst ÈĹ̿¡ src¸¦ ¿¬°á |
strncat(dst, src, n) | ÃÖ´ë n ¹®ÀÚ¸¦ src¿¡¼ dst ÈĹ̷Π¿¬°á |
strlen(s) | string sÀÇ length¸¦ return |
strcmp(s1, s2) | µÎ string s1, s2¸¦ ºñ±³ |
strncmp(s1, s2, n) | ÃÖ´ë n ¹®ÀÚ¸¸ ºñ±³ |
strchr(s, ch) | string s¿¡ Æ÷ÇÔµÈ Ã¹¹ø° ch ¹®ÀÚ pointer return (or NULL) |
strrchr(s, ch) | string s¿¡ Æ÷ÇÔµÈ ¸¶Áö¸· ch ¹®ÀÚ pointer return (or NULL) |
strstr(s1, s2) | string s2°¡ s1¿¡ Æ÷ÇԵǾî ÀÖÀ¸¸é ±× ½ÃÀÛ pointer, ¾Æ´Ï¸é NULL return |
String ÀÔ·Â
String Ãâ·Â
main(int argc, char *argv[]) // main(int argc, char **argv) argc : argumentÀÇ °¹¼ö argv[0] : ½ÇÇà ÇÁ·Î±×·¥ ÀÚ½ÅÀÇ À̸§ argv[1] : ù ¹ø° argument argv[argc - 1] : ¸¶Áö¸· argument argv[argc] : NULL pointer /***************** ½ÇÇà file¸í: echo *****************/ #includemain(int argc, char *argv[]) { while (--argc > 0) printf("%s%s", *++argv, (argc > 1) ? " " : ""); printf("\n"); return 0; } /*****************/ echo hello, world hello, world
½Ç½À 1) À§ÀÇ IsVowel functionÀ» ±¸ÇöÇÏ¿© command lineÀ¸·Î ÀÔ·ÂµÈ ¹®ÀåÀÇ °¢ ´Ü¾î¿¡¼ óÀ½ ³ª¿À´Â ¸ðÀ½µéÀ» Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó. (Sample Program)
½Ç½À 2) ÇÑ ¹®ÀåÀ» ÀÔ·Â¹Þ¾Æ °¢ ´Ü¾î¸¦ °Å²Ù·Î printÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.
½ÇÇà ¿¹: I am a girl. ==> I ma a lrig.
Ç¥ÁØÀÔ·Â(stdin)À¸·ÎºÎÅÍ ¹®ÀåµéÀ» ÀÔ·Â¹Þ¾Æ ¹®Àå¿¡ »ç¿ëµÈ ´Ü¾î¿Í °¢ ´Ü¾îÀÇ »ç¿ë ºóµµ¸¦ Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó. ´ÙÀ½ ¹®ÀåÀ» ƯÁ¤ file¿¡ ÅؽºÆ® ÇüÀ¸·Î ÀúÀåÇÏ°í ÀÌ fileÀ» input redirection ±â´ÉÀ» ÀÌ¿ëÇÏ¿© ÀÔ·ÂÇÑ´Ù.
¿¹) ÀÔ·Â
Some are born great, some achieve greatness,
and some have greatness thrust upon them.
Ãâ·Â
some : 3
are : 1
born : 1
great : 1
achieve : 1
greatness : 2
and : 1
have : 1
thrust : 1
upon : 1
them : 1