-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
159 lines (121 loc) · 2.35 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define ALPHABET_SIZE 26
typedef struct n
{
bool isEOW;
struct n *next[ALPHABET_SIZE];
}node;
node *root;
node *createNode()
{
int i;
node *n1=malloc(sizeof(node));
for(i=0;i<ALPHABET_SIZE;i++)
n1->next[i]=NULL;
n1->isEOW=false;
return n1;
}
void insertWord(char wr[])
{ node *temp=root;
int i;
for(i=0;wr[i]!='\0';i++)
{
int idx=wr[i]-'a';
if(temp->next[idx]==NULL)
temp->next[idx]=createNode();
temp=temp->next[idx];
}
temp->isEOW=true;
}
bool hasWord(char a[])
{
node *temp=root;
int i;
for(i=0;a[i]!='\0';i++)
{
int id=a[i]-'a';
if(temp->next[id]==NULL)
return false;
temp=temp->next[id];
}
return true;
}
void insert(char w[])
{
char *c;
int count=0,count1=0;
int i;
int len=strlen(w);
c=malloc(len+2);
sprintf(c,"%c%s",'~',w);
printf("\nINSERT:\n");
for(i=0;i<=len;i++)
{ char ch;
for(ch='a';ch<='z';ch++)
{
c[i]=ch;
count1++;
if(hasWord(c)==true)
{
count++;
printf("\t%s",c);
}
}
c[i]=c[i+1];
}
printf("\nfrom insert:%d\t%d\n",count,count1);
}
void replace(char w[]){
int len=strlen(w);
int i;
int count1=0;
int count=0;
printf("\nREPLACE:\n");
for(i=0;i<len;i++)
{
char c=w[i];
char ch;
for(ch='a';ch<='z';ch++)
{ if(ch!=c)
{
w[i]=ch;
if(hasWord(w)==true){count++;
printf("\t%s",w);}
count1++;
}
}
w[i]=c;
}
printf("\nfrom replace:%d\t%d\n",count,count1);
}
/*
There are many additional operations you can perform, swappping is one among them.
*/
int main()
{
int k;
char cf[50];
root=createNode();
FILE *fp;
fp=fopen("dictionary.txt", "r");
for(k=0;k<100000;k++)
{
fscanf(fp,"%s",cf);
insertWord(cf);
}
fclose(fp);
char g[50];
printf("Enter (mis-spelled)word:\n");
scanf("%s",g);
bool i=hasWord(g);
if(i==true)
printf("FOUND");
else
printf("NOT FOUND");
replace(g);
insert(g);
return 0;
}