Watch the !! birdies....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct N {
char x[20];
struct N *next;
} List;
List *insertlist(char word[15], List * b)
{
List *c = calloc(1, sizeof(List));
strcpy( c->x, word ); /*!! use strcpy for simplicity */
c->next = b;
return c;
}
/*!! unused function */
List *text(char *word)
{
List *d = NULL;
d = insertlist(word, d);
return d;
}
int search(char *query, List * z) /*!! be explicit about return types */
{
printf("pass");
while (z != NULL) {
if ( strcmp(z->x,query) == 0 ) {
printf("match");
}
z = z->next;
}
return 0;
}
int main(int argc, char *argv[])
{
char word[15];
char query[15];
int x; /*!! must be int to compare with EOF */
List *z = NULL; /*!! an empty list */
int c;
FILE *fp;
fp = fopen(argv[1], "r");
/* set things up first - get first character, set c=0 */
c = 0;
while ( (x = fgetc(fp)) != EOF) {
if (isspace(x) || ispunct(x)) {
word[c] = '\0';
/*for testing only - prints out each word */
printf("%s\n", word);
/*next char goes to start of word */
c = 0;
z = insertlist(word, z);
} else {
word[c] = x;
/*next char goes on end of word */
c = c + 1;
}
}
fclose(fp);
printf("Please enter a search query:");
fgets(query, 15, stdin);
printf("Scanned query\n");
search(query, z);
return 0; /*!! needs a return result */
}