#include <stdio.h>
char source[] = "We then handed the hat and the coat to the fat man";
int strindex(char source[], int start, char pattern[]);
main()
{
int found = 0;
int index = -1;
found = 0;
printf("the source sentence is\n'We then handed the hat and the coat to the fat man.'\n\n");
while ((index = strindex(source, index + 1, "the")) >= 0)
found++;
printf("The number of occurrences of the pattern \"the\": %d\n", found);
found = 0;
index = 100; /* start position exceeds the actual string */
while ((index = strindex(source, index + 1, "the")) >= 0)
found++;
printf("The number of occurrences of the pattern \"the\": %d\n", found);
found = 0; /* Note that, at this point, index = -1 from the previous */
while ((index = strindex(source, index + 1, "at")) >= 0)
found++;
printf("The number of occurrences of the pattern \"at\": %d\n", found);
found = 0;
while ((index = strindex(source, index + 1, "and")) >= 0)
found++;
printf("The number of occurrences of the pattern \"and\": %d\n", found);
found = 0;
while ((index = strindex(source, index + 1, "ould")) >= 0)
found++;
printf("The number of occurrences of the pattern \"ould\": %d\n", found);
getchar();
}
int strindex(char source[], int start, char pattern[])
{
int i,j,k;
for (i=start; source[i]!= '\0' ;i++){
for(j=i, k=0 ; pattern[k]!='\0' && source[j]==pattern[k]; j++,k++)
;
if(k>0 && pattern[k]=='\0')
return i;
}
return -1;
}
짜신분께문의바랍니다