#include
int bf_match(const char txt[], const char pat[])
{
int pt = 0;
int pp = 0;
while (txt[pt] != '\0' && pat[pp] != '\0') {
if (txt[pt] == pat[pp]) {
pt++;
pp++;
}
else {
pt = pt - pp + 1;
pp = 0;
}
}
if (pat[pp] == '\0')
return pt - pp;
return -1;
}
int main(void)
{
int idx;
char s1[256];
char s2[256];
puts("브루트-포스법");
printf("텍스트 : ");
scanf_s("%s", s1);
printf("패턴 : ");
scanf_s("%s", s2);
idx = bf_match(s1, s2);
if (idx == -1)
puts("텍스트에 패턴이 존재하지 않습니다.");
else
printf("%d번째 문자열부터 match합니다.\n", idx + 1);
return 0;
}
s1 입력까지는 되는데 패턴이 안뜨고 뒤져버림
int bf_match(const char txt[], const char pat[])
{
int pt = 0;
int pp = 0;
while (txt[pt] != '\0' && pat[pp] != '\0') {
if (txt[pt] == pat[pp]) {
pt++;
pp++;
}
else {
pt = pt - pp + 1;
pp = 0;
}
}
if (pat[pp] == '\0')
return pt - pp;
return -1;
}
int main(void)
{
int idx;
char s1[256];
char s2[256];
puts("브루트-포스법");
printf("텍스트 : ");
scanf_s("%s", s1);
printf("패턴 : ");
scanf_s("%s", s2);
idx = bf_match(s1, s2);
if (idx == -1)
puts("텍스트에 패턴이 존재하지 않습니다.");
else
printf("%d번째 문자열부터 match합니다.\n", idx + 1);
return 0;
}
s1 입력까지는 되는데 패턴이 안뜨고 뒤져버림