1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
int get_length(char pattern[]);
 
int main()
{
    char str[100], pattern [20];
    int count = 0, pLength = 0, i = 0;
    printf ("Enter string : ");
    gets(str);
 
    printf ("Enter the pattern : ");
    gets(pattern);
 
    do{// pac cab dsa cab : cab
            for (int j = 0; pattern[j] != '0'; j++//i=4, j=0
            {
                if (str[i] == pattern[j])
                {
                    pLength++;
                    i++;
 
                    if (pLength == get_length(pattern))
                    {
                        count++;
                        printf ("count is %d", count);
                        j = 0;
                    }
                }
                else
                {
                    i++;
                    j = 0;
                }
            }
 
}while (str[i] != '\0');
 
printf ("Pattern found %d times", count);
 
}
 
 
int get_length(char pattern[])
{
    int length = 0;
 
    for (int i = 0; pattern[i] != '\0'; i++)
    {
        length++;
    }
    return length;
}
 
cs


문자열 str과 pattern이 있어


str에는 긴 문장을 넣고 pattern에는 단어같은걸 하나 넣을거야


pattern에 들어가 있는 단어가 str 에서 몇번 반복되는지를 알아보는 프로그램인데


문제가 뭔지 모르겠어 제대로 작동을 안해


두번 반복인데도 count는 1로나오고 세번 반복도 1로 나와


루프어딘가에서 잘못된거같은데 의사선생님들 진찰한번만 해주세요