반응형

출처

http://ra2kstar.tistory.com/85

http://forum.falinux.com/zbxe/index.php?document_srl=413130&mid=C_LIB

http://forum.falinux.com/zbxe/index.php?document_srl=413133&mid=C_LIB

https://kldp.org/node/84508


모두 string.h 파일에 포함되어 있다.


1. char* strcmp(const char* s1, const char* s2) 

대소문자 구분하면서 두 개의 문자열 비교


첫 번째 문자부터 아스키 코드를 비교하여 같으면 0 반환

s1 비교문자 아스키 코드 > s2 비교문자 아스키 코드 이면 0보다 큰 값 반환

s1 비교문자 바이트 < s2 비교문자 바이트이면 0보다 작은 값 반환


윈도우에서는 단순히 1, 0, -1 값만 반환하는 반면

리눅스에서는 아스키 코드 차이 값을 반환한다.


2. char* strncmp(const char* s1, const char* s2, size_t n)

대소문자 구분하면서 두 개의 문자열의 처음 n개를 비교

strcmp()와 반환값은 동일하다.


3. char* stricmp(const char* s1, const char* s2)

대소문자 구분하지 않고 두 개의 문자열 비교


리눅스에서는 strcasecmp()라는 이름의 함수가 있는데 역할은 동일하다.

신기한 것은 여기서는 윈도우나 리눅스나 똑같이 아스키 코드 차이값을 반환한다는 것이다.


4. stricmp(const char* s1, const char* s2, size_t n)

대소문자 구분하지 않고 두 개의 문자열의 처음 n개를 비교


리눅스에서는 strncasecmp()라는 이름의 함수가 있는데 역할은 동일하다.

strcasecmp(), stricmp()의 경우처럼 

윈도우나 리눅스나 똑같이 아스키 코드 차이값을 반환한다.




윈도우



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
#include <stdio.h>
#include <string.h>
 
int main() {
    char* sl = "sophie";
    char* scl = "sop";
    char* su = "SOPHIE";
    char* scu = "SOP";
 
    char* nl = "nicole";
 
    printf("%s and %s, using strcmp() -> %d\n", sl, nl, strcmp(sl, nl));
    printf("%s and %s, using strcmp() -> %d\n", nl, sl, strcmp(nl, sl));
    printf("%s and %s, using strcmp() -> %d\n", sl, sl, strcmp(sl, sl));
    printf("%s and %s, using strncmp() -> %d\n", sl, nl, strncmp(sl, nl, 3));
    printf("%s and %s, using strncmp() -> %d\n", nl, sl, strncmp(nl, sl, 3));
    printf("%s and %s, using strncmp() -> %d\n", sl, scl, strncmp(sl, scl, 3));
    printf("%s and %s, using stricmp() -> %d\n", sl, nl, stricmp(sl, nl));
    printf("%s and %s, using stricmp() -> %d\n", nl, sl, stricmp(nl, sl));
    printf("%s and %s, using stricmp() -> %d\n", sl, su, stricmp(sl, su));  
    printf("%s and %s, using strnicmp() -> %d\n", sl, nl, strnicmp(sl, nl, 3));
    printf("%s and %s, using strnicmp() -> %d\n", nl, sl, strnicmp(nl, sl, 3));
    printf("%s and %s, using strnicmp() -> %d\n", sl, scu, strnicmp(sl, scu, 3));
    return 0;
}



리눅스



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
#include <stdio.h>
#include <string.h>
 
int main() {
    char* sl = "sophie";
    char* scl = "sop";
    char* su = "SOPHIE";
    char* scu = "SOP";
 
    char* nl = "nicole";
 
    printf("%s and %s, using strcmp() -> %d\n", sl, nl, strcmp(sl, nl));
    printf("%s and %s, using strcmp() -> %d\n", nl, sl, strcmp(nl, sl));
    printf("%s and %s, using strcmp() -> %d\n", sl, sl, strcmp(sl, sl));
    printf("%s and %s, using strncmp() -> %d\n", sl, nl, strncmp(sl, nl, 3));
    printf("%s and %s, using strncmp() -> %d\n", nl, sl, strncmp(nl, sl, 3));
    printf("%s and %s, using strncmp() -> %d\n", sl, scl, strncmp(sl, scl, 3));
    printf("%s and %s, using strncmp() -> %d\n", sl, scl, strncmp(sl, scl, 3));
    printf("%s and %s, using strcasecmp() -> %d\n", sl, nl, strcasecmp(sl, nl));
    printf("%s and %s, using strcasecmp() -> %d\n", nl, sl, strcasecmp(nl, sl));
    printf("%s and %s, using strcasecmp() -> %d\n", sl, su, strcasecmp(sl, su));
    printf("%s and %s, using strncasecmp() -> %d\n", sl, nl, strncasecmp(sl, nl, 3));
    printf("%s and %s, using strncasecmp() -> %d\n", nl, sl, strncasecmp(nl, sl, 3));
    printf("%s and %s, using strncasecmp() -> %d\n", sl, scu, strncasecmp(sl, scu, 3));
    return 0;
}

반응형
Posted by 애콜라이트
l

free counters