개념 :
1. string.h에 포함되어 있다.
2. 두문자열이 같으면 0을 리턴한다.
3. 두문자가 다르면 0이 아닌 값을 리턴한다.
strcmp 의 경우
앞의 문자열과 두번째 문자열을 하나하나 검사하는 도중 다른곳이 발견되면 멈춰서 그곳에 문자값을 아스키코드를 기준으로 검사하여
앞의 만자값이 크면 1 뒤의 아스키코드값이 크면 -1을 리턴한다.
_stricmp 의경우
먼저 대소 문자를 구분하지 않는다. 앞으문자열과 뒤의 문자열을 문자하나하나 검사하다 다르면 그곳의 값을 아스키코드값으로 리턴한다.
알파벳경우 소문자.
소스코드: main.c
#include <stdio.h>
#include <string.h>
#define MAX 256
int main(void)
{
char *pPassword = "Junmozzi";
char szBuffer[256] = { 0, };
int nPassControl = 1;
//문자 하나 하나 검색하면서 틀린곳이 발견되면
//그것의 문자값(아스키코드값을 비교) 첫번째 문자가 값이 크거나 같으면 1, 두번째 문자값이 크면 -1을 리턴한다.
//모두 일치하면 0을 리턴한다.
while (nPassControl)
{
printf("password : ");
gets_s(szBuffer, sizeof(szBuffer));
nPassControl = strcmp(pPassword, szBuffer);
printf("%d ", nPassControl);
if (nPassControl == 0)
{
puts("Match password");
}
else if (nPassControl == 1 || nPassControl == -1)
{
puts("Wrong password");
}
}
nPassControl = 1;
while (nPassControl)
{
printf("password : ");
gets_s(szBuffer, sizeof(szBuffer));
nPassControl = _stricmp(pPassword, szBuffer);//대소문자 구분없이 문자값이 맞으면 0을 다르면 1을
printf("%d, %c ", nPassControl, nPassControl);
if (nPassControl == 0)
{
puts("Match password");
}
else if (nPassControl == 1 || nPassControl == -1)
{
puts("Wrong password");
}
}
return 0;
}
결과
#c언어, #c언어입문, #프로그램입문, #strcmp, #_stricmp
'Programing - C > C Basic grammar ' 카테고리의 다른 글
061 strncmp, _strnicpm 062 strcat, strncat, strcat_s, strncat_s (0) | 2017.07.28 |
---|---|
060 strcmp, _stricmp 구현해보자 (0) | 2017.07.27 |
058 strcpy 구현해보기 (0) | 2017.07.27 |
057 strcpy, strcpy_s (0) | 2017.07.27 |
056 puts (0) | 2017.07.27 |