프로그래밍 언어/C
strcmp() 문자열 비교 함수
리그캣
2018. 1. 24. 13:51
설명
strcmp는 2개의 문자열을 비교하는 함수이며, 문자열의 길이가 크고 작음을 비교하는 것이 아니라 첫번째 문자부터 차례로 바이트의 크기를 비교한다.
헤더
string.h
형태
char* strcmp(const char *s1, const char s2);
인수
char *s1 비교할 대상 문자열
char* s2 비교할 문자열
반환
0 = 결과 값이면 s1 = s2
0 < 결과 값이면 s1 > s2
0 > 결과 값이면 s1 < s2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> #include <string.h> int main( void) { char str_apple[] = "apple"; char str_apple2[] = " apple"; char str_banana[] = "banana"; char str_appleII[]= "appleII"; printf( "%s with %s = %d\n", str_apple, str_apple , strcmp( str_apple, str_apple ) ); printf( "%s with %s = %d\n", str_apple, str_apple2 , strcmp( str_apple, str_apple2 ) ); printf( "%s with %s = %d\n", str_apple, str_banana , strcmp( str_apple, str_banana ) ); printf( "%s with %s = %d\n", str_apple, str_appleII, strcmp( str_apple, str_appleII) ); return 0; } | cs |
apple with apple = 0 apple with apple = 1 -> 공백이 있는 문자열이 더 길지만 공백문자가 'a'보다 작음 apple with banana = -1 apple with appleII = -1
출처 : http://forum.falinux.com/zbxe/index.php?document_srl=413130&mid=C_LIB