Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Spell Check Program Glitch (Please Help)

hi,
for my college course i have to write a program to spellcheck a file using a dictionary file (both read from user)

It should print a message when the word from the file is not in the dictionary.

Also, the program has to treat "dog" the same as "dog!!!" (meaning removing any non-alpbetic charactors from the end). But, of course, "d.o.g." shouldnt work.

My problem is this. Everything compiles, but when a word has non-alphabetic characters at the end, they are replaced with the '\0' character to remove them. I think this makes the word different from the one in the dictionary file because it has extra stuff at the end (sorry if that doesnt make sense).

So basically i get this as output:
(spellchecking a file containing the word "quiCK!!!" in line 1)

Unknown word "quick" found in line 1 of the input file.

even though the word "quick" IS in fact in the dictionary.

This program is due at midnight tonight and i cant figure it out.
Can anyone help me fix this glitch?

here is the code for my program(some of it may be considered "bad programming" but it works... sort of):

#include <iostream>
#include <cctype> //This library contains a file to convert to lower case
#include <fstream> // file stream
#include <cstdlib> // exit function
#include <sstream> // stringstream
#include <ctype.h> // isalpha function

using namespace std;

bool word_search(string word, string dict); //seaches dict for word
// returns true if it's there

void bad_word(const string word, const int line, ostream& out);

void removeNonAlpha(string &word); // removes non-alphabetic chars from end of word

void convertToLowerCase(string &word); //self-explanatory

int main()
{
string word, line, dict;
char file[20];
ifstream in_stream;
cout << "Enter name of file to spell check: ";
cin >> file;
in_stream.open(file);
if(in_stream.fail())
{
cout << "Failed to open file. Exiting Program. \n";
exit(1);
}
cout << "Enter name of dictionary file: ";
cin >> dict;

int lineNumber = 1;

while(getline(in_stream, line))
{
stringstream ss(line);
while(ss >> word)
{
removeNonAlpha(word);
convertToLowerCase(word);
if (!word_search(word, dict))
bad_word(word, lineNumber, cout);
}
lineNumber++;
}
}

bool word_search(string word, string dict)
{
string dictWord;
char dict2[20];

/////changes string dict to char array dict2
/////so it works with ifstream

for(int i = 0; i < 20; i++)
dict2[i] = dict[i];

ifstream in(dict2);

while(in >> dictWord) // gets word from dictionary file
{
if(dictWord == word) //checks if word is in //dictionary
{
in.close();
return true;
}
}

in.close();
return false;
}

void bad_word(const string word, const int line, ostream& out)
{
//error message
out << " Unknown word " << '"' << word << '"' << " found in line " << line << " of the input file. \n";

}

void removeNonAlpha(string &word)
{
string tmpWord = word;
int i = word.size();
while(!isalpha(word[i]))
{
tmpWord[i] = '\0';
i--;
}

word = tmpWord;
}

void convertToLowerCase(string &word)
{
int j = word.size();
for(int i = 0; i < j; i++)
word[i] = tolower(word[i]);
}
[4399 byte] By [nick53182] at [2007-11-11 8:01:54]
# 1 Re: Spell Check Program Glitch (Please Help)
if its needed, here are my "dictionary" and test files

dictionary.txt
brown
dog
fox
jumped
lazy
over
quick
the

testfile.txt

the quiCK!!! brown FOx j.umped
oVer? the L,A.Z.Y dog!!!
the qUICK BrOWN Fox JUMPED over the LaZy DOG
the;
the666
the?

pretty basic, but they do the job for testing.
nick53182 at 2007-11-11 21:01:45 >
# 2 Re: Spell Check Program Glitch (Please Help)
the problem is that the '\0' are making a difference, to fix this it changed '\0' to ' '(one space)then used this trim function if found on codeproject
void trim2(string& str)
{
string::size_type pos = str.find_last_not_of(' ');
if(pos != string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(' ');
if(pos != string::npos)
str.erase(0, pos);
}
else str.erase(str.begin(), str.end());
}

so modify your removeNonAlpha function to look like:
void removeNonAlpha(string &word)
{
string tmpWord = word;
int i = (int)word.size();
while(!isalpha(word[i]))
{
tmpWord[i] = ' '; //replace with spaces
i--;
}

trim2(tmpWord);
word = tmpWord;
}

and it should work...at least it does on my computer
Nathan87 at 2007-11-11 21:02:45 >
# 3 Re: Spell Check Program Glitch (Please Help)
thanks, that works

unfortunatly its 14 minutes late :(

oh well. I'm going to re-submit it and see if I get a better grade with part that working.
nick53182 at 2007-11-11 21:03:38 >