ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Kernighan - C] #2-8 증가 연산자와 감소 연산자
    전기전자공학/프로젝트 2017. 6. 25. 21:46


    2.8 증가 연산자와 감소 연산자

    The C Programming - Kernighan


    <예제>

    [예제 2-4]

    문자열 s2의 문자 중 문자열 s1에 있는 문자들을 지우는 squeeze(s1, s2)를 작성하라

    squeeze.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    /*
    * Filename:    squeeze.c
    * Author:        Ssong24 <pub50@naver.com>
    * Date:        25-June-2017
    *
    * The C Programming Language, Second Edition, Korean ver
    * by Kernighan and Ritchie
    *
    * Exercise 2-4, page 64
    */
     
    #include <stdio.h>
     
    #define TRUE 1
    #define FALSE 0
     
    void squeeze(char s1[], char s2[]) {
        int i, j;
        
        for (i = j = 0; s1[i] != '\0'; i++)
            if (!check(s1[i], s2))
                s1[j++= s1[i];
        s1[j] = '\0';
     
    }
     
    int check(char ch, char s2[]) {
        int state = FALSE;
        int i;
        
        for (i = 0; i < strlen(s2); i++)
        {
            if (ch == s2[i]) {
                state = TRUE;
                return state;
            }
            else state = FALSE;
        }
        return state;
    }
    cs


    main.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #include <stdio.h>
    #define MAXLINE 1000
     
    int getline(char line[], int maxline);
     
    main() {
     
        char str1[MAXLINE];
        char str2[MAXLINE];
        
        getline(str1, MAXLINE);
        getline(str2, MAXLINE);
        squeeze(str1, str2);
     
        printf("%s\n", str1);
    }
     
    int getline(char s[], int lim)
    {
        int c, i;
        for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
            s[i] = c;
     
        if (c == '\n') {
            s[i] = c;
            ++i;
        }
        s[i] = '\0';
        return i;
    }
    cs


    [예제 2-5] 

    문자열 s1에서 문자열 s2에 있는 문자들 중 하나가 나타나는 지를 찾아서 처음 나타나는 것의 위치(s1 내에서의 위치)를 리턴하고,

    없을 경우 -1을 리턴하는 함수 any(s1, s2)를 작성하라

    (표준 라이브러리 함수 strpbrk는 같은 작용을 하고, 단지 그 위치에 대한 포인터를 리턴한다.)



    댓글

Designed by Tistory.