Skip to content

Instantly share code, notes, and snippets.

/spell.c Secret

Created April 13, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b767320a86308b613c0e to your computer and use it in GitHub Desktop.
Save anonymous/b767320a86308b613c0e to your computer and use it in GitHub Desktop.
/*
* spell.c
*
* Created on: Apr 13, 2015
* Author: Brett
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
//gcc -o spell spellChecker.c
int main(int argc, char **argv[]) {
size_t size = 50;
int sizen = 100;
char fileName[] = "words.txt";
char dictName[] = "test.txt";
char tempBuffer[sizen];
char tempDictBuffer[sizen];
FILE *dictInstream = fopen(dictName, "r");
if (dictInstream == NULL) {
fprintf(stderr, "Unable to open file: %s\n", dictName);
exit(1);
}
int result = 0;
FILE *instream = fopen(fileName, "r");
if (instream == NULL) {
fprintf(stderr, "Unable to open file: %s\n", fileName);
exit(1);
}
//read the file, line by line
printf("Misspelled words in words.txt :\n");
while (fgets(tempBuffer, sizen, instream) != NULL) {
tempBuffer[strlen(tempBuffer) - 1] = '\0';
//remove the endline character from the line
char *tempToken = strtok(tempBuffer, " ");
//tokenize the line on space:
while (tempToken != NULL)
//will be null at the end of each line
{
char *tempCheck = malloc(sizeof(char) * (strlen(tempToken) + 1));
//build dynamic array to hold string to check
strcpy(tempCheck, tempToken);
while (fgets(tempDictBuffer, sizen, dictInstream) != NULL) {
//compares against each line in dictionary
tempDictBuffer[strlen(tempDictBuffer) - 1] = '\0';
//remove the endline character from the line
char *tempDict = malloc(
sizeof(char) * (strlen(tempDictBuffer) + 1));
//build dynamic array to hold string from dictionary
strcpy(tempDict, tempDictBuffer);
if (strcmp(tempCheck, tempDict) == 0) {
printf("%s, %s match\n", tempCheck, tempDict);
//if the string matches a dictionary line, this prints
result = 1;
//sets flag
} else {
printf("%s, %s do not match\n", tempCheck, tempDict);
//if the string does not match, this prints
}
free(tempDict);
tempDict = NULL;
}
if (result != 1) {
printf("%s\n", tempCheck);
//checks flag
}
result = 0;
//resets flag
free(tempCheck);
tempCheck = NULL;
tempToken = strtok(NULL, " ");
//gets next token in line and reruns second while loop
}
}
fclose(dictInstream);
fclose(instream);
return 0;
}
A
AA
AAA
thirtytwomo
this
thisaway
contains
few
words
hello
hello this contanes a few words
they are seperated by multaple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment