Guest User

Untitled

a guest
Oct 31st, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define MAX_WORD_LEN 40
  7. #define MAX_WORDS 110000
  8.  
  9. char wordlist[MAX_WORDS][MAX_WORD_LEN];
  10. // The word list from the dictionary file
  11.  
  12. void startPromptingForWords();
  13. void populateWordlist(char* wordlistFileName);
  14. bool spelledCorrectly(char* str);
  15. char* trimLineBreak(char* str);
  16. void setToLowerCase(char** str);
  17. void printStringVerbose(char* str);
  18.  
  19. int main(int argc, char** argv) {
  20.  
  21.     char* fileName;
  22.     if(argc < 2) { printf("[ERROR] An input file must be supplied\n"); exit(-1); }
  23.     if(argc > 2) { printf("[ERROR] Only one input file may be supplied\n"); exit(-1); }
  24.    
  25.     fileName = argv[1];
  26.     populateWordlist(fileName);
  27.  
  28.     startPromptingForWords();
  29.  
  30. }
  31.  
  32. void startPromptingForWords() {
  33.     // Prompts the user for words using stdin and gives them spelling recommendations
  34.  
  35.     char* word = malloc(MAX_WORD_LEN * sizeof(char));
  36.  
  37.     while(true) {
  38.  
  39.         printf("\nWord: ");
  40.         fgets(word, MAX_WORD_LEN, stdin);
  41.         word = trimLineBreak(word);
  42.         setToLowerCase(&word);
  43.         printf("Word is spelled %s\n", (spelledCorrectly(word) ? "correctly" : "incorrectly"));
  44.         printf("%s", word);
  45.  
  46.     }
  47.  
  48. }
  49.  
  50. void populateWordlist(char* wordlistFileName) {
  51.     // Copies the supplied file into the wordlist array
  52.  
  53.     FILE* wordlistFile;
  54.  
  55.     if((wordlistFile = fopen(wordlistFileName, "rb"))) {
  56.         // If the supplied file exists
  57.  
  58.         for(int i = 0; fgets(wordlist[i], MAX_WORD_LEN, wordlistFile); i++) {
  59.  
  60.             strncpy(wordlist[i], trimLineBreak(wordlist[i]), MAX_WORD_LEN);
  61.        
  62.         }
  63.         // Copy each line of the file to the wordlist array, trimming line breaks from the end of each word
  64.  
  65.     } else { printf("[ERROR] The supplied input file does not exist\n"); exit(-1); }
  66.  
  67. }
  68.  
  69. bool spelledCorrectly(char* str) {
  70.  
  71.     // for(int i = 0; i < 50; i++) printf("Word at %i: %s\n", i, wordlist[i]);
  72.  
  73.     for(int i = 0; i < 50; i++) {
  74.  
  75.         // printf("Word in list of length %li: %s\nWord to check of length %li: %s\n", strlen(wordlist[i]), wordlist[i], strlen(str), str);
  76.         printf("Verbose list word: ");
  77.         printStringVerbose(wordlist[i]);
  78.         printf("Verbose input word: ");
  79.         printStringVerbose(str);
  80.  
  81.         if(!strcmp(str, wordlist[i])) return true;
  82.    
  83.     }
  84.  
  85.     return false;
  86.  
  87. }
  88.  
  89. char* trimLineBreak(char* str) {
  90.     // Trims any trailing line breaks from a string
  91.  
  92.     int len = strlen(str);
  93.  
  94.     char* newStr = malloc((len - 1) * sizeof(char));
  95.     strncpy(newStr, str, len - 1);
  96.  
  97.     return newStr;
  98.  
  99.     // char* newStr = malloc((len + 1) * sizeof(char));
  100.     // strncpy(newStr, str, len + 1);
  101.  
  102.     // if(newStr[len - 1] == '\n') {
  103.        
  104.     //     newStr[len - 1] = '\0';
  105.     //     for(int i = len; i >= 0; i--) newStr[i + 1] = newStr[i];
  106.     //     newStr++;
  107.  
  108.     // }
  109.  
  110.     // return newStr;
  111.  
  112. }
  113.  
  114. void setToLowerCase(char** str) {
  115.     // Sets all uppercase letters to their lowercase variants in a given string
  116.  
  117.     int len = strlen(*str);
  118.  
  119.     for(int i = 0; i < len; i++) {
  120.  
  121.         if((*str)[i] >= 'A' && (*str)[i] <= 'Z') (*str)[i] |= 0b100000;
  122.  
  123.     }
  124.  
  125. }
  126.  
  127. void printStringVerbose(char* str) {
  128.  
  129.     int len = strlen(str);
  130.  
  131.     for(int i = 0; i <= len; i++) {
  132.  
  133.         if(str[i] == '\0') printf("\\0");
  134.         else if(str[i] == '\n') printf("\\n");
  135.         else if(str[i] == EOF) printf("EOF");
  136.         else printf("%c", str[i]);
  137.  
  138.     }
  139.  
  140.     printf("\n");
  141.  
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment