memcmp : 메모리 블록을 비교
strcmp : 문자열을 비교
strncmp : 문자열 개수를 지정하여 비교
2) 검색함수
memchr : 메모리 블록에서 문자열 검색
strchr : 문자열에서 찾는 문자의 첫 번째 위치
strrchr : 문자열의 마지막에서부터 문자 위치 검색
strspn : 문자열에서 특정 문자로 구성된 문자열의 길이
strcspn : 문자열에서특정문자로구성에포함되지않는문자열의길이
strstr : 부분 문자열 위치
strtok : 토큰으로 문자열 분리
# 라이브러리 함수 활용
1. 문자열 비교함수
1) memcmp
#include
#include
int main ()
{
char buffer1[] = ‚DWgaOtP12df0‛;
char buffer2[] = ‚DWGAOTP12DF0‛;
int n;
n=memcmp ( buffer1, buffer2, sizeof(buffer1) );
if (n > 0) printf (‚‘%s’ is greater than ‘%s’.\n‛,buffer1,buffer2);
else if (n < 0) printf (‚‘%s’ is less than ‘%s’.\n‛,buffer1,buffer2);
else printf (‚‘%s’ is the same as ‘%s’.\n‛,buffer1,buffer2);
return 0;
}
//결과
‘DWgaOtP12df0’ is greater than ‘DWGAOTP12DF0’
2) strcmp
#include
#include
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;
}
//결과
apple with apple = 0
apple with apple = 1
apple with banana = -1
apple with appleII = -1
3) strcmp
int main ()
{
char str[][5] = { ‚R2D2‛ , ‚C3PO‛ , ‚R2A6‛ };
int n;
puts (‚Looking for R2 astromech droids... ‛);
for (n=0 ; n < 3 ; n++) {
if (strncmp (str[n], ‚R2xx‛,2) == 0) {
printf (‚found %s\n‛,str[n]);
}
}
return 0;
}
//결과
Looking for R2 astromech droids...
found R2D2
found R2A6
2. 문자열 검색함수
1) memchr
int main ()
{
char * pch;
char str[] = ‚Example string‛;
pch = (char*) memchr (str, ‘p’, strlen(str));
if (pch!=NULL)
printf (‚ ‘p’ found at position %d.\n‛, pch-str+1);
else
printf (‚ ‘p’ not found.\n‛);
return 0;
}
//결과 확인
‘p’ found at position 5.
#include
#include
int main ()
{
int i;
char strtext[] = ‚129th‛;
char cset[] = ‚1234567890‛;
i = strspn (strtext,cset);
printf (‚The initial number has %d digits.\n‛,i);
return 0;
}
//결과
The initial number has 3 digits.
4) strcspn
#include
#include
int main ()
{
char str[] = ‚fcba73‛;
char keys[] = ‚1234567890‛;
int i;
i = strcspn (str,keys);
printf (‚The first number in str is at position %d.\n‛,i+1);
return 0;
}
//결과
The first number in str is at position 5