Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #define MAX_WORD_LEN 40
- #define MAX_WORDS 110000
- char wordlist[MAX_WORDS][MAX_WORD_LEN];
- // The word list from the dictionary file
- void startPromptingForWords();
- void populateWordlist(char* wordlistFileName);
- bool spelledCorrectly(char* str);
- char* trimLineBreak(char* str);
- void setToLowerCase(char** str);
- void printStringVerbose(char* str);
- int main(int argc, char** argv) {
- char* fileName;
- if(argc < 2) { printf("[ERROR] An input file must be supplied\n"); exit(-1); }
- if(argc > 2) { printf("[ERROR] Only one input file may be supplied\n"); exit(-1); }
- fileName = argv[1];
- populateWordlist(fileName);
- startPromptingForWords();
- }
- void startPromptingForWords() {
- // Prompts the user for words using stdin and gives them spelling recommendations
- char* word = malloc(MAX_WORD_LEN * sizeof(char));
- while(true) {
- printf("\nWord: ");
- fgets(word, MAX_WORD_LEN, stdin);
- word = trimLineBreak(word);
- setToLowerCase(&word);
- printf("Word is spelled %s\n", (spelledCorrectly(word) ? "correctly" : "incorrectly"));
- printf("%s", word);
- }
- }
- void populateWordlist(char* wordlistFileName) {
- // Copies the supplied file into the wordlist array
- FILE* wordlistFile;
- if((wordlistFile = fopen(wordlistFileName, "rb"))) {
- // If the supplied file exists
- for(int i = 0; fgets(wordlist[i], MAX_WORD_LEN, wordlistFile); i++) {
- strncpy(wordlist[i], trimLineBreak(wordlist[i]), MAX_WORD_LEN);
- }
- // Copy each line of the file to the wordlist array, trimming line breaks from the end of each word
- } else { printf("[ERROR] The supplied input file does not exist\n"); exit(-1); }
- }
- bool spelledCorrectly(char* str) {
- // for(int i = 0; i < 50; i++) printf("Word at %i: %s\n", i, wordlist[i]);
- for(int i = 0; i < 50; i++) {
- // printf("Word in list of length %li: %s\nWord to check of length %li: %s\n", strlen(wordlist[i]), wordlist[i], strlen(str), str);
- printf("Verbose list word: ");
- printStringVerbose(wordlist[i]);
- printf("Verbose input word: ");
- printStringVerbose(str);
- if(!strcmp(str, wordlist[i])) return true;
- }
- return false;
- }
- char* trimLineBreak(char* str) {
- // Trims any trailing line breaks from a string
- int len = strlen(str);
- char* newStr = malloc((len - 1) * sizeof(char));
- strncpy(newStr, str, len - 1);
- return newStr;
- // char* newStr = malloc((len + 1) * sizeof(char));
- // strncpy(newStr, str, len + 1);
- // if(newStr[len - 1] == '\n') {
- // newStr[len - 1] = '\0';
- // for(int i = len; i >= 0; i--) newStr[i + 1] = newStr[i];
- // newStr++;
- // }
- // return newStr;
- }
- void setToLowerCase(char** str) {
- // Sets all uppercase letters to their lowercase variants in a given string
- int len = strlen(*str);
- for(int i = 0; i < len; i++) {
- if((*str)[i] >= 'A' && (*str)[i] <= 'Z') (*str)[i] |= 0b100000;
- }
- }
- void printStringVerbose(char* str) {
- int len = strlen(str);
- for(int i = 0; i <= len; i++) {
- if(str[i] == '\0') printf("\\0");
- else if(str[i] == '\n') printf("\\n");
- else if(str[i] == EOF) printf("EOF");
- else printf("%c", str[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment