Guest User

Untitled

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