int lookup(char *str, char *key);
main()
{
char str[1024] = "asdfjad asdfh adsf adsf adsf adf adsfasdfkjasdf andsf akdf sdkf"
char key[32];
int pos[100];
printf("輸入要查詢的單詞: ");
scanf("%s", key);
lookup(str, key, pos);
printf("***重復了%d次\n,位置分別是:", pos[0]);
for(i = 1 ; i <= pos[0]; i ++) {
printf("%d “, pos[i]);
}
printf("\n");
}
int lookup(char *str, char *key, int pos[])
{
int i, j;
pos[0] = 0; /* 記錄重復次數 */
for(i = 0; i < strlen(str); i ++) {
if(key[0] == str[i]) {
for(j = 1; j < strlen(key); j ++) {
if(key[j] != str[i+j])
break;
}
if(j == strlen(key)) { /* found */
pos[0] ++;
pos[pos[0]] = i;
i += j-1;
}
}
}
}