CHAPTER 11
STRINGS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
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]


  1. STRING CONSTANTS
  2. STRING VARIABLES
  3. BUILDING STRINGS FROM THE INPUT
  4. STRING FUNCTIONS FROM THE STANDARD LIBRARY
  5. USING POINTERS TO TRAVERSE STRINGS
  6. CASE STUDY: ELIMINATING DUPLICATE LINES

Objectives


String: An array of characters terminated with the null character '\0'



String data ��� ���

1) Character array�� ó��

2) Pointer to character�� ó��


String str1�� str2�� copy�ϴ� function

  • str2 = str1; ==> �� ���� ���ڿ��� �����ϴ� ���� �ƴ϶� str1 pointer ���� str2 pointer ������ ������ ���̴�.

    ��� 1)

    ��� 2) while ((*s2 = *s1) != '\0') { s2++; s1++; }

    ��� 3) while ((*s2++ = *s1++) != '\0') ;

    ��� 4) while (*s2++ = *s1++) ;


    String data ��� �� ������ ����

    1) "hello"�� ���� string ����� ���ڿ��� ����� �޸� �ּҷ� ó���ȴ�.

    2) String�� array�� ���� pointer�� ����� ���̸� �ν��Ѵ�

    3) String copy �� target string�� original string�� ���Ե� ��� ���ڵ��� �����ϱ� ����� �޸𸮸� Ȯ���ϰ� �־�� �Ѵ�.

    4) String return �� local ����ó�� �Ҹ�Ǵ� �޸𸮸� ������� ���ƾ� �Ѵ�.

    �� function�� ���ڿ��� ����� �޸� �ּҸ��� return�Ѵ�. ���� ���ڿ��� ����� �޸𸮴� function ������ ������ �ڵ� �Ҹ�Ǿ� ����ȴ�. �׷��Ƿ� ���α׷� ���� �� ������ �ٲ� �� �ִ�.

    �� ���� �޸𸮸� �Ҵ�޾� ����Ͽ��� �Ѵ�.


    The ANSI string library

    "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 Input/Output

    String �Է�

    String ���


    Command-line arguments

  • main function�� arguments�� �̿��Ͽ� ���α׷� ���� �� Ư�� ������ string������ ������ �� �ִ�.


    �ǽ� 1) ���� IsVowel function�� �����Ͽ� command line���� �Էµ� ������ �� �ܾ�� ó�� ������ �������� ����ϴ� ���α׷��� �ۼ��϶�. (Sample Program)

    �ǽ� 2) �� ������ �Է¹޾� �� �ܾ �Ųٷ� print�ϴ� ���α׷��� �ۼ��϶�.

    ���� ��: I am a girl. ==> I ma a lrig.



    Assignment #9 (�Ⱓ: 1����)

    ǥ���Է�(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



    [ Table Of Contents | Previous Chapter | Next Chapter]