C program for binary search
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node { //LINKED LIST FUNCTION
char *words;
struct node *next;
} Node;
//reads single words from file also allocate needed space.
char *strdup (const char *s) {
char *d = malloc (strlen (s));
if (d == NULL) return NULL;
strcpy (d,s);
return d;
}
int main (int argc, char *argv[])
{
FILE *file1;
FILE *file2;
FILE *file3;
file1 = fopen (argv[1],"r");
file2 = fopen (argv[2],"r");
char letters [100];
char tempLetters [100][50];
//I tempLetters FOR BINARY SEARCH it has Words stored in it
Node *current , *head;
int check;
head = current = NULL;
int i = 0;
while (! feof (file1)){
fscanf (file1,"%s",letters);
Node * list = malloc (sizeof (Node));
list -> words = strdup(letters);
list -> next = NULL;
if(head == NULL){
current = head = list;
} else {
current = current->next = list;
}
// printf ("%s\n", current->words);
strcpy (tempLetters[i],current -> words);
//HERE I STORED Words in tempLetters
i++;
//i++ is for Keeping track of how many words. }
// FILE 2 reading queries:
char query [100]; //Just a local variable
int q = 0;
char QArray [100][50];//NEED THIS ONE FROM BINARY SEARCH
while (!feof (file2)){
fscanf (file2, "%s", query);
if (!feof (file2)){
//Keeping track of how many words
strcpy (QArray[q], query);
q++;
}
}
} /* //Binary Search I want to read single words from QArray and find that if there is a same word in temLetters array. and If there is then print that word and also print its ARRAY index. If there is not then read next word from QArray till QArray == NULL. I AM HAVING TROUBLE PASSING THOSE TOO ARRAYS in function. */
fclose (file1);
fclose (file2);
return 0;}