Guest User

Untitled

a guest
Oct 31st, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 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, "r"))) {
  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 - 1);
  61.             // Trim the trailing line break(s) and make sure the word is null-terminated
  62.  
  63.             printf("The word is: %s\nOf length: %li\n", wordlist[i], strlen(wordlist[i]));
  64.             printf("With hidden chars, it is: ");
  65.             printStringVerbose(wordlist[i]);
  66.        
  67.         }
  68.         // Copy each line of the file to the wordlist array, trimming line breaks from the end of each word
  69.  
  70.     } else { printf("[ERROR] The supplied input file does not exist\n"); exit(-1); }
  71.  
  72. }
  73.  
  74. bool spelledCorrectly(char* str) {
  75.  
  76.     // for(int i = 0; i < 50; i++) printf("Word at %i: %s\n", i, wordlist[i]);
  77.  
  78.     for(int i = 0; i < 50; i++) {
  79.  
  80.         printf("Word in list of length %li: %s\nWord to check of length %li: %s\n", strlen(wordlist[i]), wordlist[i], strlen(str), str);
  81.         printf("Verbose list word: ");
  82.         printStringVerbose(wordlist[i]);
  83.         printf("Verbose input word: ");
  84.         printStringVerbose(str);
  85.  
  86.         if(!strcmp(str, wordlist[i])) return true;
  87.    
  88.     }
  89.  
  90.     return false;
  91.  
  92. }
  93.  
  94. char* trimLineBreak(char* str) {
  95.     // Trims any trailing line breaks from a string
  96.  
  97.     int oldLen = strlen(str) + 1;
  98.  
  99.     int newLen = oldLen - 1;
  100.  
  101.     char* newStr = malloc(newLen * sizeof(char));
  102.     strncpy(newStr, str, newLen - 1);
  103.  
  104.     newStr[newLen - 1] = '\0';
  105.  
  106.     return newStr;
  107.  
  108. }
  109.  
  110. void setToLowerCase(char** str) {
  111.     // Sets all uppercase letters to their lowercase variants in a given string
  112.  
  113.     int len = strlen(*str);
  114.  
  115.     for(int i = 0; i < len; i++) {
  116.  
  117.         if((*str)[i] >= 'A' && (*str)[i] <= 'Z') (*str)[i] |= 0b100000;
  118.  
  119.     }
  120.  
  121. }
  122.  
  123. void printStringVerbose(char* str) {
  124.  
  125.     int len = strlen(str);
  126.  
  127.     for(int i = 0; i <= len; i++) {
  128.  
  129.         if(str[i] == '\0') printf("\\0");
  130.         else if(str[i] == '\n') printf("\\n");
  131.         else if(str[i] == EOF) printf("EOF");
  132.         else printf("%c", str[i]);
  133.  
  134.     }
  135.  
  136.     printf("\n");
  137.  
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment