[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.4

Can anybody tell me why this program's not returning the correct results?

Asked by mikeregas in C Programming Language

Tags: Microsoft, Visual C++ 2005 Express Edition, C

The name pretty much says it... I'm including the dictionary file we've been told to use as well as a sample text to check with a simple sentence. It should only say Tha is spelled wrong, but it's not.
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:
//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;
	}
}
Attachments:
 
dictionary.txt
 
 
checkfile.txt
 
[+][-]04/16/08 10:34 PM, ID: 21374395Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C Programming Language
Tags: Microsoft, Visual C++ 2005 Express Edition, C
Sign Up Now!
Solution Provided By: josgood
Participating Experts: 2
Solution Grade: B
 
[+][-]04/17/08 12:49 PM, ID: 21380672Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 / EE_QW_2_20070628