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