C언어 두 문자열 비교
#include <stdio.h>
#include <string.h>
int mystrcmp(char s[], char t[]); // mystrcmp 함수 선언
int main(void) {
// 변수 선언
char s[100];
char t[100];
int i = 0;
int j = 0;
int result;
printf("사전 순위를 비교할 첫 번째 단어를 입력하세요: ");
while ((s[i++] = getchar()) != '\n'); // null 문장
s[--i] = '\0';
printf("사전 순위를 비교할 두 번째 단어를 입력하세요: ");
while ((t[j++] = getchar()) != '\n'); // null 문장
t[--j] = '\0';
result = mystrcmp(s, t); // mystrcmp 함수 콜
printf("%d\n", result);
if (result == 1)
printf("두 번째 단어가 사전에 먼저 나옵니다.\n");
if (result == 0)
printf("두 단어가 같습니다.\n");
if (result == -1)
printf("첫 번째 단어가 사전에 먼저 나옵니다.\n");
return 0;
}
int mystrcmp(char s[], char t[])
{
int result = 0;
int i;
int cmp_len = strlen(s) < strlen(t) ? strlen(t) : strlen(s);
for (i = 0; i <= cmp_len; i++)
{
if (s[i] < t[i])
{
result = -1;
break;
}
else if (s[i] > t[i])
{
result = 1;
break;
}
else
result = 0;
}
return result;
}
코드가 엄청 짧고 간결하다... 분석이 필요하다
밑에 mystrcmp 부분 이해가 더 필요하다.
'Tech Blog > C and C++' 카테고리의 다른 글
C언어로 만드는 랜덤 UP & DOWN GAME (0) | 2021.06.08 |
---|---|
C언어 - if문 break와 continue (0) | 2021.06.08 |
C언어 반복문 (For, While, Do while) (0) | 2021.06.08 |
C언어 scanf 함수의 활용 (0) | 2021.06.07 |
C언어 논리연산자와 콤마 연산자 (0) | 2021.06.07 |