학습 내용 : 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;
}
|
실행 화면 :

'프로그래밍 > C언어 300제' 카테고리의 다른 글
중급 62. 문자열 연결하기 (strcat) (0) | 2020.02.18 |
---|---|
중급 61. 문자열을 일정 크기만큼 비교하기 (strncmp) (0) | 2020.02.18 |
중급 59. 문자열을 대.소문자로 구분하여 비교하기 (strcmp) (0) | 2020.02.17 |
중급 58. 문자열을 복사하는 함수 만들기 (0) | 2020.02.17 |
중급 57. 문자열 복사하기(strcpy) (0) | 2020.02.17 |
트랙백 , 댓글 가 달렸습니다.