#include <stdio.h>
#include <string.h>
#include <assert.h>
int len(char* str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
void init(int* i, int* j, int* finded)
{
*i = 0;
*j = 0;
*finded = 0;
}
void increase_finded(int* finded, int* j)
{
(*finded) += 1;
(*j)++;
}
void init_i(int* finded, int* i, int* j)
{
if (*finded >= 1)
*i = 0;
*finded = 0;
}
char* ft_strstr(char* str, char* to_find)
{
int i;
int j;
int finded;
init(&i, &j, &finded);
if (len(to_find) == 0)
return str;
while (to_find[i] != '\0')
{
while (str[j] != '\0')
{
if (to_find[i] == str[j])
{
increase_finded(&finded, &j);
break;
}
else
init_i(&finded, &i, &j);
j++;
}
i++;
}
if (finded == len(to_find))
return str + j - finded;
return 0;
}
int main(void)
{
// 테스트 케이스 1: 일반 케이스 - 부분 문자열 찾기
char haystack1[] = "Hello, world!";
char needle1[] = "world";
char* result1 = ft_strstr(haystack1, needle1);
assert(result1 != NULL);
assert(strcmp(result1, "world!") == 0);
// 테스트 케이스 2: 일반 케이스 - 부분 문자열이 맨 앞에 있는 경우
char haystack2[] = "Hello, world!";
char needle2[] = "Hello";
char* result2 = ft_strstr(haystack2, needle2);
assert(result2 != NULL);
assert(strcmp(result2, "Hello, world!") == 0);
// 테스트 케이스 3: 일반 케이스 - 부분 문자열이 맨 끝에 있는 경우
char haystack3[] = "Hello, world!";
char needle3[] = "!";
char* result3 = ft_strstr(haystack3, needle3);
assert(result3 != NULL);
assert(strcmp(result3, "!") == 0);
// 테스트 케이스 4: 일반 케이스 - 빈 부분 문자열 찾기 (haystack 반환)
char haystack4[] = "Hello, world!";
char needle4[] = "";
char* result4 = ft_strstr(haystack4, needle4);
assert(result4 == haystack4);
// 테스트 케이스 5: 일반 케이스 - 부분 문자열을 찾을 수 없는 경우 (NULL 반환)
char haystack5[] = "Hello, world!";
char needle5[] = "plaonet";
char* result5 = ft_strstr(haystack5, needle5);
assert(result5 == NULL);
// 테스트 케이스 6: 일반 케이스 - 여러 번 등장하는 경우, 첫 번째 것을 찾기
char haystack6[] = "Mississippi";
char needle6[] = "iss";
char* result6 = ft_strstr(haystack6, needle6);
assert(result6 != NULL);
assert(strcmp(result6, "ississippi") == 0);
// 테스트 케이스 7: 특수 문자 포함된 경우
char haystack7[] = "abc!@#def";
char needle7[] = "!@#";
char* result7 = ft_strstr(haystack7, needle7);
assert(result7 != NULL);
assert(strcmp(result7, "!@#def") == 0);
// 테스트 케이스 8: 문자열에 공백이 있는 경우
char haystack8[] = "find the needle in the haystack";
char needle8[] = "needle";
char* result8 = ft_strstr(haystack8, needle8);
assert(result8 != NULL);
assert(strcmp(result8, "needle in the haystack") == 0);
// 테스트 케이스 9: needle이 haystack보다 긴 경우
char haystack9[] = "short";
char needle9[] = "longerneedle";
char* result9 = ft_strstr(haystack9, needle9);
assert(result9 == NULL);
// 테스트 케이스 10: 빈 haystack
char haystack10 [] = "";
char needle10[] = "something";
char* result10 = ft_strstr(haystack10, needle10);
assert(result10 == NULL);
// 테스트 케이스 11: haystack에 반복 문자가 있는 경우
char haystack11[] = "aaaaaab";
char needle11[] = "b";
char* result11 = ft_strstr(haystack11, needle11);
assert(result11 != NULL);
assert(strcmp(result11, "b") == 0);
printf("모든 테스트를 통과했습니다.\n");
}
To embed this program on your website, copy the following code and paste it into your website's HTML: