入力された文字列を (コマンド ライン引数として) 受け取り、文字列に含まれる動詞を出力するプログラムを作成しようとしています。動詞のリストは、別のヘッダー ファイルの配列にあります。プログラムは現在動詞を見つけていますが、同じ単語を複数回出力しています。これが私のコードです:
#include "verbs.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void binary_search(char *list_of_words, int size, char *target){
int bottom= 0;
int mid;
int top = size - 1;
while(bottom <= top){
mid = (bottom + top)/2;
if (strcmp(&list_of_words[mid], target) == 0){
printf("%s found at location %d.\n", target, mid+1);
break;
}
if (strcmp(&list_of_words[mid], target) == 1){
top= mid - 1;
}
if (strcmp(&list_of_words[mid], target) == -1){
bottom= mid + 1;
}
}
}
int main(int argc, char* argv[]){
char *input;
int i = 0;
input = strtok (argv[1], " \"\n");
while (input != NULL){
for (i = 0; i < VERBS; i++){ //VERBS is defined in verbs.h as 637
binary_search(verbs[i], VERBS, input);
}
input = strtok (NULL, " ");
}
return 0;
}
何か案は?