Hi yall i needed to write a program that reads all the words from a file and stores the unique words in a linked list and counting how many of them appear in the text. Here's my code but it seems to be a bit buggy and doesn't compile
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
# define MAX 10000
typedef struct node{
char word[50];
int count;
struct next *next;
} node;
node * makeNode( char arr[]);
node * insertWord( char arr[],node* top);
node * findPrevious (char arr[],node*top)
void printWords(node * top);
node * searchList (char arr[],node *top);
//===========================MAIN================================//
int main (){
FILE * in=fopen("paragraph.txt","r");
int i=0,n,j;
char str[MAX];
node*top=NULL;
node*found;
if(in==NULL){
printf ("File not found\n");
}
else {
while ((fscanf (in,"%s" ,& str[i]))!=EOF){
fscanf (in,"%s" ,& str[i]);
i++;
}
for (n=0;n<i;n++){
str[n]=tolower (str[n]);
} //converts all the elements in the array to lower case
for (j=0;j<i;j++){
found= searchList(str[j],top);//searchList function to search the list to see if the word previously occured
if (found==NULL){
top=insertWord(str[j],top);/*insert the word in the list maintain sorted order*/
}
else {
(found->count)++;// increment count since the word has previously occured before
}
} //end for loop
printList(top);
system ("pause");
return 0;
} //end main
//=====================================END MAIN=====================================//
//==================================================================================//
node* makeNode( char arr[n]){
/* function to make a new node to store the word*/
node* newNode;
newNode= (node*) malloc (sizeof (node));
strcpy(newNode->word,arr[n]);
newNode-> count=1;
newNode->next=NULL;
return newNode;
}//end makeNode
//==================================================================================//
node* insertWord( char arr[n],node* top){
/* inserts the word in the list (maintaining sorted order) and return the address of top */
node * previous;
node * newNode;
newNode=makeNode(arr[n]);
if (top==NULL){
top=newNode;
}
else{
previous= findPrevious(arr[n],top);
if (previous==NULL){
newNode->next=NULL;
top=newNode;
}
else{
newNode->next=previous->next;
previous->next=newNode;
}
}
return top;
} /*end insertWord*/
//==================================================================================//
node* findPrevious (char arr[n],node*top){
//function to find where in the list to insert the new node created
node * prev;
node * curr;
while (curr!=NULL){
if ((strcmp (curr->word,arr[n])>0){// if the word on the linked list is higher in alphabetical value do//
return prev;
}
else{
prev=curr;//save previous before moving on
curr=curr->next;
}
} //end while
return prev;
} // end findPrevious function
//==================================================================================//
void printWords(node * top) {
/* prints all the words in the list 1 per line*/
node * curr;
curr=top;
while (curr->next!=NULL){
printf(" The word ' %s ' occurs %d times\n\n", curr-> word, curr-> count);
curr=curr->next;
}//end while
}//end printLIst
//==================================================================================//
node * searchList (char arr[int n],node *top){
/* searches the list for a given word*/
node *curr;
*curr=*top;
while (curr!=NULL){
if ((strcmp (curr->word,arr[n]))==0) { //if the word has occured in the list
return curr;
}
curr=curr->next;
} //end while
return NULL;//if the word has not occured in the list already
} // end searchList
these are the errors i am getting:
F:\Assignment 2\Assignment.c In function `findPrevious':
21 F:\Assignment 2\Assignment.c syntax error before '{' token
25 F:\Assignment 2\Assignment.c parameter `i' is initialized
27 F:\Assignment 2\Assignment.c parameter `top' is initialized
32 F:\Assignment 2\Assignment.c syntax error before "if"
95 F:\Assignment 2\Assignment.c syntax error before "newNode"
125 F:\Assignment 2\Assignment.c syntax error before "while"
I am really at a point where i don't know what to do anymore. I can't seem to debug it myself
Any help will be greatly appreciated