-
-
Save anonymous/b767320a86308b613c0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
AA | |
AAA | |
thirtytwomo | |
this | |
thisaway | |
contains | |
few | |
words | |
hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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