ABOUT ME

Today
Yesterday
Total
  • 두 문자열 결합 (정렬이 되어있다고 가정)
    코딩/C언어 2019. 3. 21. 14:02
    반응형
    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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    #include <stdio.h>
    void mergeString(char a[], char b[], char result[])
    {
        int i, num1, num2, n1 = 0, n2 = 0;
     
        for (i = 0; a[i] != '\0'; i++);
     
        num1 = i;
     
        for (i = 0; b[i] != '\0'; i++);
     
        num2 = i;
     
        for (i = 0; i <= num1 + num2 + 1; i++)
        {
            if (i == num1 + num2 + 1)
                result[i] = '\0';
            else if (n1 >= num1)
            {
                result[i] = b[n2];
                n2++;
            }
            else if (n2 >= num2)
            {
                result[i] = a[n1];
                n1++;
            }
            else if (a[n1] <= b[n2])
            {
                result[i] = a[n1];
                n1++;
            }
            else if (a[n1] > b[n2])
            {
                result[i] = b[n2];
                n2++;
            }
        }
     
        return;
    }
    int main(void)
    {
        char word1[10], word2[10];
        char mergedWord[20];
     
        scanf("%s %s", word1, word2);
     
        mergeString(word1, word2, mergedWord);
     
        printf("%s\n", mergedWord);
        return 0;
    }
    cs

     

     

    결과:

     

     

    반응형

    '코딩 > C언어' 카테고리의 다른 글

    난수 발생 및 난수의 합, 최대값  (0) 2019.03.21
    문자열 속의 숫자의 합  (0) 2019.03.21
    문자열 거꾸로 출력  (0) 2019.03.19
    SNS 2촌 계산  (0) 2019.03.18
    피보나치 수열 계산 (재귀함수)  (0) 2019.03.18
Designed by Tistory.