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

개념 : 


_strset_s 과 같은 개념이다. 다른점은 표시할 구역을 정할수있다.



목표 : 


기존 _strset_s을 예제 소스를 _strnst_s으로 바꿔보자


소스코드 : main.c

#include <stdio.h>

#include <string.h>

#include <stdlib.h>


int main(void)

{

char *Password = "helloMozzi;


char szBuffer[256] = { 0, };

char szShow[256] = { 0, };

int character = '0';

int count = 0;

int Run = 1;

while (Run)

{

printf("password :%s", szShow);

character = getch();// 문자 입력 받고


if (character == 8) // backspace

{

if (count > 0)

{

count--;

szBuffer[count] = '\0';

}

}

else

{

if (character != 13)

szBuffer[count] = character;

}


if (count < 256)

{

szBuffer[count + 1] = '\0';

}

else

{

count = -1;

}





if (character == 13)// 엔터인지 검사하고

{

if (strcmp(szBuffer, Password) == 0)

{

puts("\nmatch password");

Run = 0;

}

else

{

memset(szBuffer, 0, sizeof(szBuffer));

}

count = -1;

}


system("cls");

memcpy(szShow, szBuffer, sizeof(szShow));


//< 이구간을 좀 변형 ㅎ

if (count > 0)

_strnset_s(szShow, sizeof(szShow), '*', count);


if (character == 8)

{


}

else

{

if (count >= 0)

szShow[count] = character;


++count;

}


}


return 0;

}



결과



#c언어, #c언어입문, #프로그램입문, #_strnset_s, #문자가리는코드


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

072 _strlwr_s  (0) 2017.07.28
071 _strupr_s  (0) 2017.07.28
069 _strset_s  (0) 2017.07.28
068 strpbrk  (0) 2017.07.28
067 strtok_s  (0) 2017.07.28
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념 : 


string.h 에포함됬어 있으며 문자열을 특정 문자로 변환해주는 함수



목표 : 


패스워드 입력 받고 맞으면 프로그램이 종료되는 프로그램을 만들어 보자


소스코드 : Main.c

#include <stdio.h>

#include <string.h>

#include <stdlib.h>


int main(void)

{

char *Password = "Mozzi";


char szBuffer[256] = { 0, };

char szShow[256] = { 0, };

int character = '0';

int count = 0;

int Run = 1;

while (Run)

{

printf("password :%s", szShow);

character = getch();// 문자 입력 받고


if (character == 8) // backspace

{

if (count > 0)

{

count--;

szBuffer[count] = '\0';

}

}

else

{

if (character != 13)

szBuffer[count] = character;

}


if (count < 256)

{

szBuffer[count + 1] = '\0';

}

else

{

count = -1;

}





if (character == 13)// 엔터인지 검사하고

{

if (strcmp(szBuffer, Password) == 0)

{

puts("\nmatch password");

Run = 0;

}

else

{

memset(szBuffer, 0, sizeof(szBuffer));

}

count = -1;

}


system("cls");

memcpy(szShow, szBuffer, sizeof(szShow));


//< 이구간을 좀 변형 ㅎ

_strset_s(szShow, sizeof(szShow), '*');


if (character == 8)

{


}

else

{

if (count >= 0)

szShow[count] = character;


++count;

}


}


return 0;

}



결과 



#C언어, #C언어입문, #프로그램입문, #_strset_s, #암호프로그램




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

071 _strupr_s  (0) 2017.07.28
070 _strnset_s  (0) 2017.07.28
068 strpbrk  (0) 2017.07.28
067 strtok_s  (0) 2017.07.28
066 strcspn, strspn 활용과 구현  (0) 2017.07.28
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념 : 

1. string.h 에포함


2. strtok_s와 동일한 기능을한다 다만 토큰셋 자리에 문자 널을 넣어주지 않는다. 그래서 만약 반환리턴값을 


계속 사용할경우 바로찾아서 그자리를 계속 리턴하여 무한 반복문이 될수있음에 주의해서사용해서해야한다.


3. String Point break 약자




소스코드 : main.c

#include <stdio.h>

#include <string.h>


int main(void)

{

char pString[] = "안녕하세요  이준모 입니다.  만나서 반갑습니다 ^^  기분  좋은  하루  되세요^^";

char*pFind = " ";

char*pPos = pString;

char*Token = pString;


printf("ALL : ");

puts(Token);

puts("");


while (1)

{

//puts(pString);

Token = strpbrk(Token, pFind);

if (Token == NULL)

{

break;

}

printf("token:");

puts(Token++);// 찾는 문자열의 크기만큼 증가하지 않음녀 계속 같은자리를 반복하는문제점을 가질수있다.

puts("");

}


return 0;

}



결과



#c언어, #c언어입문, #프로그램입문, #strpbrk

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

070 _strnset_s  (0) 2017.07.28
069 _strset_s  (0) 2017.07.28
067 strtok_s  (0) 2017.07.28
066 strcspn, strspn 활용과 구현  (0) 2017.07.28
065 strchr를 사용해보고 구현해보자  (0) 2017.07.28
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념 : 


1. string.h 포함


2. 문자열에서 구분되는 문자셋의 내용을 찾아서 널로 변환한다.


3. parameter


첫번째 인자값 : 문자열


두번째 인자값 : 구분문자셋


세번째 인자값 :  구분되어지고 그앞의 주소값


리턴값 : 구분되어 떨어저 나온 문자열의 첫번째 주소


4. 문자열에 끝에 도달하면 세번째 인자값은 널이 된다.




소스코드 : main.c

#include <stdio.h>

#include <string.h>


int main(void)

{

char pString[] = "안녕하세요 이준모 입니다. 만나서 반갑습니다 ^^ 기분 좋은 하루 되세요^^";

char*pFind = " ";

char*pPos = pString;

char*Token = NULL;


printf("ALL : ");

puts(pPos);

puts("");

while (*pPos != '\0')

{

//puts(pString);

Token = strtok_s(pPos, pFind, &pPos);

printf("stirng:");

puts(pPos);

printf("token:");

puts(Token);

puts("");

}


return 0;

}



결과


#c언어입문, #프로그램입문, #c언어, #strtok, #strok_s



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

069 _strset_s  (0) 2017.07.28
068 strpbrk  (0) 2017.07.28
066 strcspn, strspn 활용과 구현  (0) 2017.07.28
065 strchr를 사용해보고 구현해보자  (0) 2017.07.28
064 strstr  (0) 2017.07.28
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념:


1. string.h 에 포함


2. 원형 : 


 unsigned int strcspn(const char* string, const char* strCharSet)


 unsigned int strspn(const char* string, const char* strCharSet)


3. strcspn : 문자열중 일치하는 첫번째 문자 위치를 검색 


4. strspn : 문자열중 일치하지 않는 첫번째 문자 위치를검색


5. 만약 결과가 없다면 문자널이 일치하는곳을 찾아 리턴해준다.


목표:


사용해보고 만들어보자


소스코드:main.c


#include <stdio.h>

#include <string.h>


unsigned int MYstrcspn(const char* string, const char* strCharSet);

unsigned int MYstrspn(const char* string, const char* strCharSet);


int main(void)

{

const char * string = "000000000010000";

const char* strCharSet = "1111111111111111";

int result = -1;


unsigned int strlength = strlen(string);

//1을 찾으면 주소값을 리턴하게 만들 예제를 만들어보조


puts("strcspn 호출");

result = strcspn(string, strCharSet);


if (result == -1)

{

puts("찾기 실패");

}

else if (result < strlength)

{

printf("문자열에서 첫번째로 일치하는 문자를 찾았습니다. 주소값 : %d \n", result);

}



result = -1;

//1을 찾으면 주소값을 리턴하게 만들 예제를 만들어보조


puts("strspn 호출");

result = strspn(string, strCharSet);


if (result == -1)

{

puts("찾기 실패");

}

else if (result < strlength)

{

printf("문자열에서 첫번째로 일치하지 않는 문자를 찾았습니다. 주소값 : %d \n", result);//첫번째 위치 0 입니다.

}



//구현해본다.

result = -1;


puts("MYstrcspn 호출");

result = MYstrcspn(string, strCharSet);


if (result == -1)

{

puts("찾기 실패");

}

else if (result < strlength)

{

printf("문자열에서 첫번째로 일치하는 문자를 찾았습니다. 주소값 : %d \n", result);

}



result = -1;

//1을 찾으면 주소값을 리턴하게 만들 예제를 만들어보조


puts("MYstrspn 호출");

result = MYstrspn(string, strCharSet);


if (result == -1)

{

puts("찾기 실패");

}

else if (result < strlength)

{

printf("문자열에서 첫번째로 일치하지 않는 문자를 찾았습니다. 주소값 : %d \n", result);//첫번째 위치 0 입니다.

}



return 0;

}


unsigned int MYstrcspn(const char* string, const char* strCharSet)

{

int i = 0, j = 0;

int IsRun = 1;


while (IsRun)

{

while (IsRun)

{

if (string[i] == strCharSet[j])

{

IsRun = 0;

}


if (j == strlen(strCharSet))

{

j = 0;

break;

}


j++;


}

i++;

}


return i - 1; // 문자열은 0부터 시작하기때문에

}

unsigned int MYstrspn(const char* string, const char* strCharSet)

{

int i = 0, j = 0;

int IsRun = 1;


while (IsRun)

{

while (IsRun)

{

if (string[i] == strCharSet[j] || j == strlen(strCharSet))

{

break;

j = 0;

}

else

{

IsRun = 0;

}


j++;

}

i++;

}


return i - 1; // 문자열은 0부터 시작하기때문에

}



결과



#c언어, #c언어입문, #프로그램입문, #strcspn, #strspn, #strcspn구현, #strspn구현

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

068 strpbrk  (0) 2017.07.28
067 strtok_s  (0) 2017.07.28
065 strchr를 사용해보고 구현해보자  (0) 2017.07.28
064 strstr  (0) 2017.07.28
063 strlen 사용및 구현  (0) 2017.07.28
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

개념 : 


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


2. 문자열에서 특정 문자를 찾아준다.




목표 : 


사용해보고 구현해보자^^


소스코드 : main.c

#include<stdio.h>

#include<string.h>





char* MYstrchr(char*pSource, unsigned char ucFind);


int main(void)

{

char *_STRING = "aaaaaaaaSaaaaaaa";

const int  _CHARACTER = 'S';

char *pFind = NULL;

int nCount = 0;

int nLength = strlen(_STRING);

char *pTemp = NULL;

pFind = strchr(_STRING, _CHARACTER);


if (pFind != NULL) //find!!

{

while (nCount < nLength)

{

pTemp = &_STRING[nCount];


if (pFind == pTemp)

{

break;

}

nCount++;

}


printf("%s  에서 %c 는 %d번째 있고 주소는 %d입니다.\n", _STRING, _CHARACTER, nCount, pFind);

}

else

{

puts("Not find!");

}

/*----------------------------------------------------------------------------------------------------*/

puts("----------------------------------");

nCount = 0;

pFind = NULL;

pTemp = NULL;


pFind = MYstrchr(_STRING, _CHARACTER);


if (pFind != NULL) //find!!

{

while (nCount < nLength)

{

pTemp = &_STRING[nCount];


if (pFind == pTemp)

{

break;

}

nCount++;

}


printf("%s  에서 %c 는 %d번째 있고 주소는 %d입니다.\n", _STRING, _CHARACTER, nCount, pFind);

}

else

{

puts("Not find!");

}


return 0;

}


char* MYstrchr(char*pSource, unsigned char ucFind)

{

char *pFind = NULL;

int nCount = 0;


while (1)

{

if (pSource[nCount] == ucFind)

{

pFind = &(pSource[nCount]);

break;

}

nCount++;

}


return pFind;

}



결과



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

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

067 strtok_s  (0) 2017.07.28
066 strcspn, strspn 활용과 구현  (0) 2017.07.28
064 strstr  (0) 2017.07.28
063 strlen 사용및 구현  (0) 2017.07.28
061 strncmp, _strnicpm 062 strcat, strncat, strcat_s, strncat_s  (0) 2017.07.28
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구현, #문자열검색


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

개념 : 


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


2. 파라메터로 문자열의 첫 번째 주소를 넣어준다.


3. 순수하게 문자갯수를 파악해준다.(문자열 끝을 알려주는 '\0'문자널은 포함하지 않는다.)




목표 : 


strlen 사용과 mystrlen을 구현해본다.




소스코드 : main.c

#include <stdio.h>

#include <string.h>


int MYstrlen(char* pString)

{

int count = 0;


while (1)

{

if (pString[count] == '\0')

break;

else

count++;

}


return count;

}


int main(void)

{

char* pString = "Hello All";


printf("call strlen : %s :%d", pString, strlen(pString));

puts("");

printf("call MYstrlen : %s :%d", pString, MYstrlen(pString));

puts("");



return 0;

}




결과



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




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

061 strncmp, _strnicpm


개념 : 


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


2. strcmp, _stricmp 와 같은점은 문자열검사를 하는것이고 다른것이 앞에서부터 몇바이트를 검사할것인지를 지정해준다.



목표: 


명령인지를 체크하고 그명령문이라면 프로그램이 멈추는 기능을 구현해보자.




소스코드 : main.c

#include <stdio.h>

#include <string.h>


#define TRUE 1

#define FALSE 0

#define MAX 256


int main(void)

{

char *pString = "Cmd";

char szBuffer[MAX] = { 0, };

int nMatchByte = strlen(pString);

int nMatch = 0;


while (TRUE)

{

printf("input : ");

gets_s(szBuffer, sizeof(szBuffer));

nMatch = strncmp(pString, szBuffer, nMatchByte);


if (nMatch == 0)

{

puts("Matched Cmd");

break;

}

else

{

printf("%d ", nMatch);

puts("error code");

}

}


puts("Checked CMD");

puts(szBuffer);


memset(szBuffer, '\0', sizeof(szBuffer));// 버퍼 공간을 초기화해준다.


while (TRUE)

{

printf("input : ");

gets_s(szBuffer, sizeof(szBuffer));

nMatch = _strnicmp(pString, szBuffer, nMatchByte);


if (nMatch == 0)

{

puts("Matched Cmd");

break;

}

else

{

printf("%d ", nMatch);

puts("error code");

}

}


puts("Checked CMD");

puts(szBuffer);




return 0;

}



결과


#strncmp, #_strnicmp




062 strcat, strncat, strcat_s, strncat_s


개념 : 


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


2. 비쥬얼 스튜디오 2010 이상 버전 부터는 _s를 붙여서 사용한다.


3. 첫번째 문자열에 두번째 문자열값을 복사 해주는데 첫번째 문자열 끝부분을 연결하여 붙여준다.



목표 : 


입력한 문자들을 메모리에 저장하고 exit 라고 입력하기전까지 기록한다.



소스코드 : main.c

#include <stdio.h>

#include <string.h>


#define TRUE 1

#define FALSE 0


#define BUFFER_MAX 1024

#define TEMP_MAX 256

#define MAX_CHARACTERS 5


int main(void)

{

char szDest[BUFFER_MAX] = { 0, };

char szTemp[TEMP_MAX] = { 0, };


int nCount = 0;

puts("strcat_s");

while (TRUE)

{

printf("input (progream stop exit): ");

gets_s(szTemp, sizeof(szTemp));


if (_stricmp("exit", szTemp) == 0)

{

puts("exit, storp program");

break;

}

else

{

nCount += strlen(szTemp);


if (nCount >= sizeof(char) * BUFFER_MAX)

{

puts("memory full, stop program");

break;

}

else

{

strcat_s(szDest, sizeof(szDest), szTemp);

strcat_s(szDest, sizeof(szDest), "\n");//개행을 넣어준다.

}


}

}


puts(szDest);


memset(szDest, 0, sizeof(szDest)); // 버퍼 초기화


nCount = 0;

puts("strncat_s");

while (TRUE)

{

printf("input (progream stop exit): ");

gets_s(szTemp, sizeof(szTemp));


if (_stricmp("exit", szTemp) == 0)

{

puts("exit, storp program");

break;

}

else

{

nCount += 5;


if (nCount >= sizeof(char) * BUFFER_MAX)

{

puts("memory full, stop program");

break;

}

else

{

strncat_s(szDest, sizeof(szDest), szTemp, MAX_CHARACTERS); // 특징은 5개만 뽑아서 연결해준다.

strcat_s(szDest, sizeof(szDest), "\n");//개행을 넣어준다.

}


}

}

puts(szDest);

return 0;

}




결과




#c언어, #c언어입문, #프로그램입문, #strcat, #strncar, #strcat_s, #strncat_s

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

064 strstr  (0) 2017.07.28
063 strlen 사용및 구현  (0) 2017.07.28
060 strcmp, _stricmp 구현해보자  (0) 2017.07.27
059 strcmp, _stricmp  (0) 2017.07.27
058 strcpy 구현해보기  (0) 2017.07.27
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