C 언어 문자열 함수 strstr() 사용한 문자열 찾기

#include <stdio.h>
#include <string.h>

int main()
{
        char str_source[1024] = {0};
        char str_find[1024] = {0};
        char *pFind = NULL;
        int count = 0;

        printf("첫 번째 문자열은 ? ");
        scanf("%s",str_source);

        printf("찾고 싶은 문자열은 ? ");
        scanf("%s",str_find);

        if(strlen(str_source) <= 0 || strlen(str_find) <= 0 )
        {
                printf("공백 문자열은 안되요!\n");
                return 0;
        }

        pFind = strstr(str_source, str_find);
        while (NULL != pFind)
        {
                count++;
                pFind = strstr(pFind + strlen(str_find), str_find);
        }

        printf("문자열 \"%s\" 에는 문자열 \"%s\" 이 %d번 있습니다\n", str_source, str_find, count);

        return 0;
}


실행시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./find_string_input
첫 번째 문자열은 ? 남동길입니다.남동길이에요.안녕하세요!남동길이니까!남동길이에요!
찾고 싶은 문자열은 ? 남동길
문자열 "남동길입니다.남동길이에요.안녕하세요!남동길이니까!남동길이에요!" 에는 문자열 "남동길" 이 4번 있습니다

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

Spring Boot Actuator readiness, liveness probes on k8s

About idempotent

About ZGC

About G1 GC

sneak peek jitpack

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

HackerRank Java Between Two Sets

Java - HashMap (feat. LinkedList, Tree.. maybe Later)