Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "fix_text.h"
- int cmp(const void *w1, const void *w2)
- {
- char *elem1 = *(char **)w1;
- char *elem2 = *(char **)w2;
- return strcmp(elem1, elem2);
- }
- void spellCorrect(char **lex, size_t n, char **sent)
- {
- int ind = 0, start_ind = 0;
- while ((*sent)[ind] != '\0')
- {
- // get 1 word
- start_ind = ind;
- while ((*sent)[ind] != ' ' && (*sent)[ind] != '\0')
- ind++;
- // copy word to cur_word
- int word_len = ind - start_ind;
- char *cur_word = malloc(word_len + 1);
- memcpy(cur_word, (*sent) + start_ind, word_len);
- cur_word[word_len] = '\0';
- // search if cur_word is in lex;
- void *ptr = bsearch(&cur_word, lex, n, sizeof(char *), cmp);
- // if found do nothing, it's correct!
- if (ptr)
- {
- ind++;
- continue;
- }
- // else substitue last char
- int base_ind = ind - 1;
- int left_len = strlen((*sent) + base_ind + 1);
- // copy everything from next space till the end into the current word's last char
- memcpy((*sent) + base_ind, (*sent) + base_ind + 1, left_len + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement