[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!

7.2

using fgets to read one line at a time

Asked by mikeregas in C Programming Language

I have a program that is intended to be a spell checker and I am trying to get it to read one line at time so that the output for the program is correct. It does read the file properly and return the misspelled words, however they are all on line 1. I need it to read into the paragraph and return the different lines and I have no idea how to do that.

Here is a sample output for the file

The following words were not recognized:
Line 5 - compter
Line 8 - sciance
Line 23 - oone

the code for the program is attached
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:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
 
//CONSTANTS 
#define wrdlen 48 
#define linelen 1024
 
 
// A struct representing a node in a binary search tree
 
struct BSTnode
{  
char word[wrdlen];// The contents of the node	
struct BSTnode* left;// Links to the node's left and right children
struct BSTnode* right;
 
};
 
// 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 BSTnode** root, char newword[wrdlen])
{
    // If we've reached the right place to insert, create a new node and add it in
	if( (*root) == NULL)
	{
		(*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode));
        strcpy((*root)->word,newword);
		(*root)->left = NULL;
		(*root)->right = NULL;
		return 1;
	}
    // Otherwise, search for the correct place to insert
 
	if(strcmp(newword,(*root)->word)<0)
	{
		return insert( &((*root)->left), newword);
	}
 
	else if(strcmp(newword,(*root)->word)>0)
	{
		return insert( &((*root)->right), newword);
	}
// 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;
}
 
// Returns 1 if target is in the tree and 0 otherwise
int search(struct BSTnode* root, char target[wrdlen])
{
    // 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);
}
 
// An iterative version of the search algorithm
int searchiterative(struct BSTnode* root, char target[wrdlen])
{
	struct BSTnode* crnt = root; 
	// Keep descending through the tree until we reach the bottom or find what
	// we're looking for
	while(crnt != NULL && strcmp(crnt->word,target)!=0)
	{
		if(strcmp(target,crnt->word)>0)
		crnt = crnt->left;
 
		else
		crnt = crnt->right;
	}
	// If we reached the bottom of the tree, then the target isn't present,
	// otherwise we found what we're looking for
	if(crnt == NULL)
		return 0;
	
	else
		return 1;
}
 
void spellcheck(struct BSTnode* root, char *token, int line)
{
    //capital letters are from 65 -> 90
    //lowercase letters are from 97-122  
    if(search(root, token))//if you find it normally then 
      return;
 
     else if(token[0] >= 65 && token[0] <= 90)
     {
          token[0] = token[0] + 32;
          if(search(root, token))
             return;
 
          else
          {   
              token[0] = token[0] - 32;
              printf("Line %d: %s\n", line, token); 
              return;
          }
     }
 
     else
     {
         printf("Line %d: %s\n", line, token);
         return;
     }
}
 
 
int main(void)
{
    FILE *ifp; //dictionary file
    FILE *scfp; //file to be spellchecked
    char infile[wrdlen]; //"words.txt";
    char Tinfile[wrdlen]; //"test.txt"; //test file
    int valid = 0;
	struct BSTnode* root = NULL;
    //struct BSTnode* root;
    char newword[wrdlen];
    int i = 0;
    char str[linelen];
	char delims[] = {"~!@#$%^&*()-_=+[]{}\\|;:\'\",.<>/?\n\r\t "};
	char *token;
	
     
 
    //ask the user for the dictionary file
    while(!valid)
    {
         printf("Please enter the name of the dictionary file you wish to access\n");
         scanf("%s", infile);
         ifp = fopen(infile, "r");         
 
         if(ifp == NULL)
           printf("sorry, could not find that file!\n");
         else
         {   
             valid = 1;
             printf("Reading file now.........\n");
         }
    }    
    valid = 0;
    //read in all the words and place them into a BST
    while(!feof(ifp))
    {
        fscanf(ifp, "%s ", newword);
        insert(&root, newword);
    }   
 
    //ask the user for the file to be spellchecked
    while(!valid)
    {
		printf("what is the name of the file you would like to spellcheck?\n");
		scanf("%s", Tinfile);
		scfp = fopen(Tinfile, "r");
		if(scfp == NULL)
           printf("sorry, could not find that file!\n");
		else
		{   
             valid = 1;
             printf("Reading file now.........\n");
		}
    }    
 
    printf("The following words were not recognized: \n");    
 
    for(i = 1; !feof(scfp); i++)
    {
     	fgets(str, linelen, scfp);
		
		token = strtok(str, delims);          
 
		while( token != NULL ) 
		{
			spellcheck(root, token, i);    
			token = strtok( NULL, delims );
	    }    
    }
	system("PAUSE");
    return 0; 
}
[+][-]11/11/08 06:44 AM, ID: 22930895Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]11/11/08 06:49 AM, ID: 22930942Accepted 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
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 1
Solution Grade: B
 
[+][-]11/11/08 06:50 AM, ID: 22930954Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]11/11/08 07:09 AM, ID: 22931156Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]11/11/08 07:45 AM, ID: 22931538Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]11/11/08 08:15 AM, ID: 22931843Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]11/11/08 08:18 AM, ID: 22931863Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]11/11/08 08:56 AM, ID: 22932273Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]11/11/08 08:57 AM, ID: 22932287Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

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

 
[+][-]11/11/08 09:04 AM, ID: 22932363Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

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