본문 바로가기

TEST/자료구조와 함께 배우는 알고리즘 입문(C언어)

<8> Q7. 자료구조와 함께 배우는 알고리즘 입문 (C언어)

#include <stdio.h>
#include <string.h>

int str_ncmp(const char *s1, const char *s2, size_t n)
{
    for(size_t i = 0; i<n;i++){
        if(s1[i]!=s2[i])
            return s1[i]-s2[i];
    }
    return 0;
}

int main(void)
{
    char st[128];
    puts("\"STRING\"의 처음 3개의 문자와 비교합니다.");
    puts("\"XXXX\"를 입력하면 종료합니다.");
    while (1)
    {
        printf("문자열 st : ");
        scanf("%s", st);
        if (str_ncmp("XXXX", st, 3) == 0)
            break;
        printf("str_ncmp(\"STRING\",st,3) = %d\n", str_ncmp("STRING", st, 3));
    }

    return 0;
}