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

개념 :


1.string.h에 포함 되어있다.


2. 문자열에서 특정 문자열을찾아내느 함수이고 찾으면 그첫번째 주소를 반환한다.




목표 :  


구현해보자.. 그리고 실재 사용 하고 비슷 한지도 체크해보자!




소스코드 :Main.c

#include <stdio.h>

#include <string.h>


char* MYstrstr(const char *pString, const char *pSub);


int main(void)

{

char *pExample = "Hello Hello Hello Hello Hello Junmozzi Hello Hello Hello Hello Hello";

char *pJunmozzi = "Junmozzi";

char *pObject = NULL;

char *pMyObject = NULL;


int nStrLength = strlen(pJunmozzi);//문자열 길이 미리 추출

int nCount = 0;


pObject = strstr(pExample, pJunmozzi); // 문자열, 검색할 단어

pMyObject = MYstrstr(pExample, pJunmozzi);


if (pObject == NULL)

{

puts("Not find!!!");

}

else

{

printf("String : %s \n", pExample);

puts("");

printf("Sub String : %s \n", pJunmozzi);

printf("find!! \n");


for (nCount = 0; nCount < nStrLength; nCount++)

{

putch(pObject[nCount]);//문자열 찾았는지 확인해보기 위해서 문자 하나씩을 출력

}


}


if (pMyObject == NULL)

{

puts("Not find!!!");

}

else

{

printf("String : %s \n", pExample);

puts("");

printf("Sub String : %s \n", pJunmozzi);

printf("find!! \n");


for (nCount = 0; nCount < nStrLength; nCount++)

{

putch(pMyObject[nCount]);//문자열 찾았는지 확인해보기 위해서 문자 하나씩을 출력

}


}



return 0;

}


char* MYstrstr(const char *pString, const char *pSub)

{

int nStringLen = strlen(pString);

int nSubLen = strlen(pSub);

int nCount = 0;

char szBuffer[256] = { 0, };


char *pReturnValue = NULL;



if (nStringLen >= nSubLen)//1. 단계검사 길이 검사

{

//2. 단계 같은지 검사

for (nCount = 0; nCount < nStringLen; nCount++)

{

if (pString[nCount] == *pSub)// 첫번째 문자가 똑같다면

{

// 문자열을 복사하는데~ sub길이만큼만 복사해준다. 이때 멈춘 위치에서부터 카피가 시작한다.

strcpy_s(szBuffer, sizeof(szBuffer), &(pString[nCount]));

szBuffer[nSubLen] = '\0';// 잘라준다.


//검사한다.

if (strcmp(szBuffer, pSub) == 0)// 같냐?!

{

pReturnValue = &(pString[nCount]);

break;

}


}

}

}



return pReturnValue;

}





결과




#c언어입문, #c언어, #프로그램입문, #strstr, #strstr구현, #문자열검색


+ Recent posts