aboutsummaryrefslogtreecommitdiff
path: root/6.1.1/key.c
diff options
context:
space:
mode:
Diffstat (limited to '6.1.1/key.c')
-rw-r--r--6.1.1/key.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/6.1.1/key.c b/6.1.1/key.c
new file mode 100644
index 0000000..f6384fe
--- /dev/null
+++ b/6.1.1/key.c
@@ -0,0 +1,23 @@
+#include <string.h>
+#include "key.h"
+
+struct key *
+bsearch(char *word, struct key tab[], int n)
+{
+ int cond;
+ struct key *low, *high, *mid;
+
+ low = tab;
+ high = tab + n - 1;
+ while (low <= high) {
+ mid = low + (high-low) / 2;
+ if ((cond = strcmp(word, mid->word)) < 0)
+ high = mid - 1;
+ else if (cond > 0)
+ low = mid + 1;
+ else
+ return mid;
+ }
+
+ return NULL;
+}