//HEADERS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//CONSTANTS
#define STRING_SIZE 64
#define LINE_LENGTH 1024
//FILE POINTERS
FILE *dfp=NULL; //Dictionary file pointer
FILE *cfp=NULL; //Check file pointer
//STRUCTS
//A struct representing a node in a binary search tree
struct TreeNode
{
//The contents of the node
char Word[STRING_SIZE];
//Links to the node's left and right children
struct TreeNode* left;
struct TreeNode* right;
};
//FUCTION PROTOTYPES
int Insert(struct TreeNode** Root, char NewData[STRING_SIZE]);
int Search(struct TreeNode** Root, char Target[STRING_SIZE]);
void SpellCheck(struct TreeNode** Root, char kill[STRING_SIZE], int line);
//MAIN
void main(void)
{
//Variable delarations
char User_DictFile[STRING_SIZE], User_CheckFile[STRING_SIZE];
char crnt[LINE_LENGTH], *kill;
struct TreeNode* Root;
int Line_Count=0, i=0;
Root = NULL;
// system("CLS");
//Dictionary File
do
{
printf("What is the name of your dictionary file? ");
scanf("%s", User_DictFile);
dfp = fopen(User_DictFile,"r"); //Open the specified file for reading
//File not found!
if (dfp == NULL)
{
printf("Sorry, the file does not exist.\n ");
}
}while(dfp == NULL);
//Spellcheck File
do
{
printf("What is the name of your spellcheck file? ");
scanf("%s", User_CheckFile);
cfp = fopen(User_CheckFile,"r"); //Open the specified file for reading
//File not found!
if (cfp == NULL)
{
printf("Sorry, the file does not exist.\n ");
}
}while(cfp == NULL);
//Processing the dictionary file
do
{
fgets(crnt,STRING_SIZE,dfp); //Read 1 line at a time
Insert(&Root,crnt); //Insert each word read from the line into our BST
}while(feof(dfp)==0); //Do this until the end of the file is reached
printf("Dictionary Successfully Processed!\n");
system("PAUSE");
printf("These words were not recognized: \n");
//Looping for spellchecking the check file
i=1;
do
{
fgets(crnt,LINE_LENGTH,cfp); //Read 1 line at a time
kill = strtok(crnt," ~!@#$%^&*()-_=+[]{}\\|;:\'\",./?\n\r\t");
while (kill != NULL)
{
SpellCheck(Root, kill, i);
kill = strtok (NULL, " ~!@#$%^&*()-_=+[]{}\\|;:\'\",./?\n\r\t");
}
i++;
}while(!feof(cfp));
printf("Checkfile Successfully Processed!\n");
system("PAUSE");
//Close the files
fclose(dfp);
fclose(cfp);
system("PAUSE");
}
//INSERT
//Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a
//new node was added, returns 0 if newdata was already in the tree
int Insert(struct TreeNode** Root, char NewData[STRING_SIZE])
{
//If we've reached the right place to insert, create a new node and add it in
if( (*Root) == NULL)
{
(*Root) = (struct TreeNode*)malloc(sizeof(struct TreeNode));
strcpy( (*Root)->Word,NewData );
(*Root)->left = NULL;
(*Root)->right = NULL;
return 1;
}
//Otherwise, search for the correct place to insert
if( strcmp(NewData,(*Root)->Word)<0 )
{
return Insert( &((*Root)->left), NewData);
}
else if( strcmp(NewData,(*Root)->Word)>0 )
{
return Insert( &((*Root)->right), NewData);
}
//If the new data is neither less than nor greater than the the data at
//the current node, it must be equal, and duplicates are not allowed
else
{
return 0;
}
}
//SEARCH
//Returns 1 if target is in the tree and 0 otherwise
int Search(struct TreeNode* Root, char Target[STRING_SIZE])
{
//An empty tree contains nothing, much less target
if(Root == NULL)
{
return 0;
}
//If the current node is what we're looking for, we've found it
if(strcmp(Root->Word, Target)==0)
{
return 1;
}
//If what we're looking for is smaller than this node, it must be in
//the left subtree if it exists
if(strcmp(Target,Root->Word)<0)
{
return Search(Root->left, Target);
}
//Similarly, if the target is greater than this node, it can only be in
//the right subtree
else
{
return Search(Root->right, Target);
}
}
//SPELLCHECK
//Here is the actual spell-checking algorithm
void SpellCheck(struct TreeNode** Root, char kill[STRING_SIZE], int line)
{
if(Search(Root,kill))
{
return;
}
else if(kill[0]>=65 && kill[0] <=90)
{
kill[0] = kill[0] + 32;
if(Search(Root, kill))
{
return;
}
else
{
kill[0] = kill[0] - 32;
printf("Line %d: %s\n", line, kill);
return;
}
}
else
{
printf("Line %d: %s\n", line, kill);
return;
}
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
by: josgoodPosted on 2008-04-16 at 22:34:35ID: 21374395
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
); //Read 1 line at a time
Trim the trailing newline, as in
fgets(crnt,STRING_SIZE,dfp
if (strlen(crnt) > 0) crnt[strlen(crnt)-1] = 0;
so that a word extracted from the checkfile will match a word in the dictionaryfile.