ì¶œì œìž�ì�˜ 뜻ì�´ strstr함수를 ì�´ìš©í•˜ëŠ” 것ì�´ 아니ë�¼
strstr, strcase, strspn등과 비슷한 함수를 ì œìž‘í•˜ëŠ” 것ì�´ì—ˆë‹¤ë©´ ì�´ 답ì�€ í‹€ë ¸ë‹¤.
strstr, strcase, strspn등과 비슷한 함수를 ì œìž‘í•˜ëŠ” 것ì�´ì—ˆë‹¤ë©´ ì�´ 답ì�€ í‹€ë ¸ë‹¤.
~cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
// while ê³ ì¹˜ê¸° 귀찮다. whileì�„ 무한 루프 ë�Œë¦¬ê³ Ctrl-C를 ì •ìƒ�ì �ì�¸ 종료로 바꿔 ë��ë‚´ìž�.
void handler(int sig)
{
fprintf(stderr, "\n프로그램� 종료합니다.\n");
exit(0);
}
//
int main()
{
FILE *fp;
char x[40] = "His teaching method is very good.";
char buf[10];
char *loc;
// signal handling
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
//
if((fp = fopen("result.out", "w")) == NULL)
fprintf(stderr, "fopen() error: result.out\n"), exit(-1);
while(1){
fprintf(fp, "�료 -> %s\n", x);
fprintf(fp, "찾� 문�열 -> ");
printf("찾� 문�열 -> ");
fgets(buf, sizeof(buf), stdin); // 문ìž�ì—´ì�´ë�¼ê³ 했으니 space를 í�¬í•¨í•œë‹¤.
buf[strlen(buf)-1] = '\0'; // stream으로 ë°›ì�€ \nì�„ ì œê±°í•œë‹¤.
fprintf(fp, "%s\n", buf);
if((loc = strstr(x, buf)) == 0)
fprintf(fp, "Not Found!\n");
else
fprintf(fp, "first found -> %d\n", loc-x);
fflush(fp);
}
fclose(fp);
return 0;
}
~cpp // result.out �료 -> His teaching method is very good. 찾� 문�열 -> method first found -> 13 �료 -> His teaching method is very good. 찾� 문�열 -> test Not Found!










