tags:

views:

54

answers:

2

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 && tempArticle[i] <= 64)
      tempArticle[i] = ' ';
     i++;
    }

    strcpy(tempArticle[i], dictionary);

    printf("%s", tempArticle);

    free(tempArticle);
}
A: 

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..

Richie
Regarding the last strcpy(): strcpy() iterates over the input string. Thus, if the above even compiles, the result would be writing 'n' characters into some unknown memory address. *Crash*
nimrodm
+1  A: 

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.

caf