forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellchecker.c
More file actions
122 lines (104 loc) · 2.97 KB
/
spellchecker.c
File metadata and controls
122 lines (104 loc) · 2.97 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORDS 1000
#define MAX_LEN 100
// Function to devowel a word
void devowel(const char *word, char *out) {
int n = strlen(word);
for (int i = 0; i < n; i++) {
char c = tolower(word[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
out[i] = '*';
else
out[i] = c;
}
out[n] = '\0';
}
// Function to lowercase a word
void toLowerStr(const char *src, char *dst) {
int n = strlen(src);
for (int i = 0; i < n; i++) {
dst[i] = tolower(src[i]);
}
dst[n] = '\0';
}
// Check if string exists in list
int findExact(char words[][MAX_LEN], int size, const char *target) {
for (int i = 0; i < size; i++) {
if (strcmp(words[i], target) == 0)
return i;
}
return -1;
}
// Check lowercase match
int findLower(char lowers[][MAX_LEN], int size, const char *target) {
for (int i = 0; i < size; i++) {
if (strcmp(lowers[i], target) == 0)
return i;
}
return -1;
}
// Check devowel match
int findDevowel(char devs[][MAX_LEN], int size, const char *target) {
for (int i = 0; i < size; i++) {
if (strcmp(devs[i], target) == 0)
return i;
}
return -1;
}
void spellchecker(
char wordlist[][MAX_LEN], int wordCount,
char queries[][MAX_LEN], int queryCount,
char output[][MAX_LEN]
) {
char lowers[MAX_WORDS][MAX_LEN];
char devs[MAX_WORDS][MAX_LEN];
// Preprocessing wordlist
for (int i = 0; i < wordCount; i++) {
toLowerStr(wordlist[i], lowers[i]);
devowel(wordlist[i], devs[i]);
}
// Processing queries
for (int i = 0; i < queryCount; i++) {
char *q = queries[i];
// 1. Exact match
int idx = findExact(wordlist, wordCount, q);
if (idx != -1) {
strcpy(output[i], wordlist[idx]);
continue;
}
// 2. Case-insensitive match
char lowQ[MAX_LEN];
toLowerStr(q, lowQ);
idx = findLower(lowers, wordCount, lowQ);
if (idx != -1) {
strcpy(output[i], wordlist[idx]);
continue;
}
// 3. Devowel match
char dvQ[MAX_LEN];
devowel(q, dvQ);
idx = findDevowel(devs, wordCount, dvQ);
if (idx != -1) {
strcpy(output[i], wordlist[idx]);
continue;
}
// 4. No match
strcpy(output[i], "");
}
}
int main() {
// Example usage
char wordlist[MAX_WORDS][MAX_LEN] = {"KiTe", "kite", "hare", "Hare"};
char queries[MAX_WORDS][MAX_LEN] = {"kite", "Kite", "KiTe", "Hare", "HARE", "Hear", "hear", "keti", "keet", "keto"};
int wordCount = 4;
int queryCount = 10;
char output[MAX_WORDS][MAX_LEN];
spellchecker(wordlist, wordCount, queries, queryCount, output);
for (int i = 0; i < queryCount; i++) {
printf("Query: %-5s -> Result: %s\n", queries[i], output[i]);
}
return 0;
}