/*-----------------------------------------------------------------*\
	TRYST Problem #2
	Spell Checker
	---------------

	Compiles under Platform :

	Turbo C++
	------------
	A computer program to correct mis-spelt words.
	Word list file required in ASCII format.

									Designed and programmed by:
									   ASHISH GUPTA
									   A-2, Jwalamukhi
									   98131
									   B.Tech
									   COMPUTER SCIENCE AND ENGINEERING
									   IITD
\*-----------------------------------------------------------------*/

#include <fstream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>

//_____________Global Functions___________
int Match(char* s1,char *s2);
void Animate(char* s,int x,int y);
void Normal();
void Reverse();
void Intro();

char* s2;
//___________Main Program starts here____________
void main()
{

Normal();
clrscr();
char filename[80];
ifstream infile;

Animate("Spell Check",36,1);
Normal();
textcolor(7);

int i;

for(i=1;i<=80;i++)
{
	gotoxy(i,2);
	cputs("Í");
}

cout << endl;
Intro();
cout << endl;

while(1)
{
	cout << "\nThe wordlist should be in ASCII format\n";
	cout << "Please enter the wordlist filename ( including path ) : ";

	cin.getline(filename,80);
	infile.open(filename);
	if(infile==0)
	{
		cout << "\nFatal Error : Error opening file";
	}
	else
	{
		cout << "\nSuccess : The wordlist was found.";
		break;
	}

}

cout << endl;
char word[80];
char buffer[80];
int result,t;

s2=new char[80];

while(1)
{

	cout << endl ;
	cputs( "Please enter the word : ");
	result=0;

	cin.getline(word,80);

	infile.close();
	infile.open(filename);

	cout << "\nChecking for occurence...";
	char alter=1;


	while(infile)
	{
		infile.getline(buffer,80);

		t=Match(buffer,word);
		switch(t)
		{
			case 1:
				cout << "\nThe word input by the user is PRESENT in the list.";
				cout << "\n\nDo you still want to check for alternative words ??";
				cout << "\nExample :  best --> bets.";
				while(!(alter=='y' || alter=='n'))
				{
					cout << "\nEnter Choice (y/n) : ";
					alter=tolower(getche());
				}

				if(alter=='y')
					alter=1;
				else
					alter=0;


				break;
		}
		if(t==1) result=1;
	}

	if(result==0)
	{
		cout << "\nThe word input by the user is NOT PRESENT in the list.\n";
	}

	if(result==0 || alter==1)
	{
	infile.close();
	infile.open(filename);

	result=0;
	cout << "\n\nChecking for alternatives...";

	while(infile)
	{
		infile.getline(buffer,80);

		t=Match(buffer,word);
		switch(t)
		{
			case 2:
				cout << "\nAlternative word : ";
				cout << buffer;
				break;
		}
		if(t==2) result=1;
	}

	if(result==0)
	{
		cout << "\nNo Alternatives were found.";
	}
	}

	char choice=1;
	while(!(choice=='y' || choice=='n'))
	{
		cout << "\n\nWant to check another word ( y/n ) ? ";
		choice=tolower(getche());
	}

	cout << endl << endl;
	for(i=1;i<=80;i++)
	{
		cout << "Í";
	}

	if(choice!='y') break;
}


}

int Match(char* buffer,char *s1)
{

if(strlen(s1)!=strlen(buffer)) return 0;

strcpy(s2,buffer);

int i;
char flag=1;

for(i=0;i<strlen(s1);i++)
{

	//_________A character is not matching______
	s1[i]=tolower(s1[i]);
	s2[i]=tolower(s2[i]);

	if(s1[i]!=s2[i])
	{
		if(i==(strlen(s1)-1) ) // _________if last character_____
		{
			flag=0;
			break;
		}
		else
		{
			s1[i+1]=tolower(s1[i+1]);
			s2[i+1]=tolower(s2[i+1]);

			if(!(s1[i]==s2[i+1] && s1[i+1]==s2[i]))
			{
				flag=0;
				break;
			}
			else
			{
				flag=2;
				i++;
			}
		}
	}
}


return flag;


}

void Animate(char* s,int x,int y)
{

int i,j,l;
char buffer[2];
buffer[1]=NULL;

l=strlen(s);

for(i=l-1;i>=0;i--)
{
	for(j=1;j<=(i+x-1);j++)
	{
		buffer[0]=s[i];
		Reverse();
		gotoxy(j,y);
		cputs(buffer);

		sound(200+j*50);
		delay(1);
		nosound();

	   if(j<(i+x-1))
	   {
			gotoxy(j,y);
			Normal();
			cputs(" ");
	   }
	}
}

Normal();
}

void Reverse()
{
	textcolor(0);
	textbackground(7);
}

void Normal()
{
	textcolor(7);
	textbackground(0);
}

void Intro()
{

cout << "\nWelcome to the Spell Checker-Corrector !";
cout << "\n\nThis program checks spelling and even corrects misspelt words";
cout << "\nif adjacent letters are dearranged ( Suggests alternate words )";
cout << "\nExample :  anticipaet ----> anticipate";
cout << "\n\nNote : A valid ASCII format wordlist file must be provided.";
cout << endl;
for(int i=1;i<=80;i++)
{
	cout << "Ä";
}

}
