U E D R , A S I H C RSS

이영호/문자열검색

strstr함
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!
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:32
Processing time 0.0086 sec