학습 내용 : stcmp() 함수의 내부적인 흐름을 학습합니다.

 

소스 코드 :

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
 
#define SKY "sky"
 
int My_strcmp(const char* string1, const char* string2);
 
void main()
{
    char string[100];
    int ret;
 
    printf("영단어를 입력한 후 Enter키를 치세요!\n");
    printf("sky를 입력하면 프로그램이 종료됩니다.\n");
 
    do
    {
        gets(string);
 
        ret = My_strcmp(string, SKY);
 
        if (ret == 0)
        {
            printf("%s == %s, ret = %d\n"string, SKY, ret);
            break;
        }
        else if (ret < 0)
        {
            printf("%s < %s, ret = %d\n"string, SKY, ret);
        }
        else
        {
            printf("%s > %s, ret = %d\n"string, SKY, ret);
        }
    } while (1);
}
 
int My_strcmp(const char* string1, const char* string2)
{
    if (*string1 == (int)NULL && *string2 == (int)NULL)
    {
        return 0;
    }
 
    while (*string1 != (int)NULL)
    {
        if (*string2 == (int)NULL)
        {
            return 1;
        }
 
        if (*string1 == *string2)
        {
            string1++;
            string2++;
            continue;
        }
 
        if (*string1 < *string2)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
 
    if (*string2 != (int)NULL)
    {
        return -1;
    }
 
    return 0;
}

 

 

실행 화면 :