开发者

Matching words from a dictionary(an external txt file) with an article (another external txt file)

I've managed to store the article into heap, but what should I do with the dictionary? I tried using strcpy(tempArticle[i], dictionary); but it didn't seem to work? would someone give me a hint as to what to do in the next step?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void spellCheck(char article[], char dictionary[]) {
    int len = strlen(article) + 1;
    int i;
    char* tempArticle;
    tempArticle = malloc(len);

    if (tempArticle == NULL) {
        printf("spellcheck: Memory allocation failed.\n");
        return;
    }

    for(i = 0; i < len; i++)
        tempArticle[i] = tolower(article[i]);

        i=0;

    while (tempArticle[i] != '\0'){
        if (tempArticle[i] >= 33 &&a开发者_Go百科mp; tempArticle[i] <= 64)
            tempArticle[i] = ' ';
        i++;
    }

    strcpy(tempArticle[i], dictionary);

    printf("%s", tempArticle);

    free(tempArticle);
}


Your best bet will be to figure out your algorithm first, and then code it up.

In other words, get a whiteboard, a piece of paper or fire up a text editor and write down, in plain language, the steps you would go through to compare the article against the dictionary. You might start with something very simple, like:

  • Go through each word in the article, and see if it is in the dictionary. If is it not, add it to a list of misspelled words.
  • Print the list of misspelled words.

Then you have to break each of the complicated bits down further - for example, "see if a single word is in the dictionary" needs to be broken down. (By the way, each of the self-contained "complicated bits" like this is a good candidate for a separate function in the resulting code).

At that point, you will find that converting what you've come up with into code will be a lot easier.


I tried using strcpy(tempArticle[i], dictionary); but it didn't seem to work?

tempArticle[i] is an array position and dictionary is an array..

Use array for storing array.. hopefully that will solve the issue.. Right now i guess only the 1st character in dictionary will be stored in temparticle..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜