/* 실습) 주어진 두 문자열의 사전상의 순서를 비교 해 주는 함수 mystrcmp(s, t)를 만드시오 */
#include <stdio.h>
#include <string.h>
int cus_strcmp(char[], char[]);
int main(void)
{
char s[50];
char t[50];
int i = 0;
int j = 0;
int total;
printf("첫 번째 문자열 입력: \n");
while ((s[i++] = getchar()) != '\n');
s[--i] = '\0';
printf("두 번째 문자열 입력: \n");
while ((t[j++] = getchar()) != '\n');
t[--j] = '\0';
total = cus_strcmp(s, t);
if (total == 1) {
printf("두 번째 문자열이 사전상 먼저임\n");
}
else if (total == -1) {
printf("첫 번째 문자열이 사전상 먼저임\n");
}
else {
printf("두 문자열은 동일함 \n");
}
return 0;
}
int cus_strcmp(char s[], char t[])
{
int len_s = strlen(s), len_t = strlen(t);
int bigger = 0;
int i = 0;
if (len_s > len_t) {
bigger = len_s;
}
else {
bigger = len_t;
}
for (i = 0; i < bigger; i++) {
if (s[i] > t[i]) {
return 1;
}
else if (s[i] < t[i]) {
return -1;
}
else {
continue; // continue위치에 return 0; 을 넣고, 그 다음 줄에 return을 넣으면 망한 코드가 된다.(이유는 모름)
}
return 0;
}
}
'c&c++' 카테고리의 다른 글
전역변수 vs. 지역변수 (0) | 2021.09.30 |
---|---|
break는 '반복문'을 탈출한다 (0) | 2021.09.26 |
scanf함수 이야기 (0) | 2021.09.14 |
printf함수: 서식 문자를 정돈 및 정렬하여 출력하기 (0) | 2021.09.14 |
자료형의 변환 (0) | 2021.09.14 |