1

因此,我尝试使用二进制搜索在数组中的单词词典中查找某个单词。目前我的功能是这样的,

void binsearch(char letters[], char changeletters[], char **dictionary[], int numwords){

printf("start binsearch\n");
int max = numwords;
int min = 0;
int mid;
int spot;

while (min <= max){

printf("start while in search\n");

    mid = (min + max) / 2;
    spot = strcmp(**dictionary[mid],changeletters);
    printf("%d", spot);

    if (spot == 0){
        printf("word found\n");
        printf("A permutation of %s that is a valid word is %s", letters, changeletters);
    }
    else if (spot < 0){
        printf("word not found and word is lower\n");
        min=spot+1;
    }
    else{
        printf("word no found and word is higher\n");
        max=spot-1;
    }
 }

     if( max > min)
         printf("Sorry, no permutations of %s form a word", letters);
}

随机 printfs 用于检查程序的运行距离。目前它在字符串比较中失败,我不知道为什么。有任何想法吗?

4

0 回答 0