336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

목표 : 

strcmp와 _stricmp를 구현해보자.



소스코드: main.c

#include <stdio.h>


#define TRUE 1

#define FALSE 0


#define LOW_TO_UPPER 32

#define MAX 256


int strcmp(char* pString1, char *pString2);

int _stricmp(char* pString1, char *pString2);


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;

}


int strcmp(char* pString1, char *pString2)

{

int nResultValue = 0;

int nCount = 0;


while (TRUE)

{

if (pString1[nCount] == pString2[nCount])

{

if (pString1[nCount] == '\0')

{

nResultValue = 0;

break;

}

}

else

{

nResultValue = pString1[nCount] >= pString2[nCount] ? 1 : -1;

break;

}


nCount++;

}


return nResultValue;

}


int _stricmp(char* pString1, char *pString2)

{

int nResultValue = 0;

int nCount = 0;

int nConvertLow1 = 0;

int nConvertLow2 = 0;

//pString2[nCount] 

//pString1[nCount] 


while (TRUE)

{

//일단 값을 카피한다.

nConvertLow1 = pString1[nCount];

nConvertLow2 = pString2[nCount];


//대문자 인지 검사해줘서 조건에 해당하면 소문자로 변환해주는 작업을해준다.

if (nConvertLow1 >= 'A' &&  nConvertLow1 <= 'Z')

{

nConvertLow1 += LOW_TO_UPPER;

}

if (nConvertLow2 >= 'A' &&  nConvertLow1 <= 'Z')

{

nConvertLow2 += LOW_TO_UPPER;

}


//변환된 대상으로 검사를 진행한다.

if (nConvertLow1 == nConvertLow2)

{

if (nConvertLow1 == '\0')

{

nResultValue = 0;

break;

}

}

else

{

nResultValue = nConvertLow1;

break;

}


nCount++;

}


return nResultValue;

}



결과


#c언어입문, #c언어, #프로그램입문, # strcmp구현, #_stricmp구현

'Programing - C > C Basic grammar ' 카테고리의 다른 글

063 strlen 사용및 구현  (0) 2017.07.28
061 strncmp, _strnicpm 062 strcat, strncat, strcat_s, strncat_s  (0) 2017.07.28
059 strcmp, _stricmp  (0) 2017.07.27
058 strcpy 구현해보기  (0) 2017.07.27
057 strcpy, strcpy_s  (0) 2017.07.27

+ Recent posts