[c언어] 두개의 문자열을 비교하는 함수(strcmp, strncmp)

|



두개의 문자열을 비교하는 함수(strcmp, strncmp)에 대해 알아보는 프로그램

#include <stdio.h>
#include <string.h>
int main(void)
{
    char *s1="Republic of KOREA";
    char *s2="Republic of CHINA";
    int ptr;
    ptr=strcmp(s1, s2);
    printf("strcmp결과\n");
    if (ptr>0)
        printf("문자열 s1이 s2보다 크다.\n");
    else if (ptr<0)
        printf("문자열 s1이 s2보다 작다.\n");
    else
        printf("문자열 s1과 s2가 같다.\n");
    ptr=strncmp(s1, s2, 12);
    printf("strncmp결과\n");
    if (ptr>0)
        printf("문자열 s1이 s2보다 크다.\n");
    else if (ptr<0)
        printf("문자열 s1이 s2보다 작다.\n");
    else
        printf("문자열 s1과 s2가 같다.\n");
    return 0;
}

// 실행 결과



  ::