Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

https://faculty.utrgv.edu/emmett.tomai/courses/1370/04_arrays/largedictionary.tx

ID: 3751506 • Letter: H

Question

https://faculty.utrgv.edu/emmett.tomai/courses/1370/04_arrays/largedictionary.txt

Topics: Files, loops, functions, arrays

Write a program that repeatedly asks the user to enter a word.  After each word is entered, the program tells the user if the word is correctly spelled or not.

Sample run:

Welcome to the spell checker!  Please enter a word:

> apple

The word apple is spelled correctly!

Enter another word:

> snake

The word snake is spelled correctly!

Enter another word:

> compooter

WHOA!  compooter isn’t a word!  Bad human! Learn to spell!

> dinosore

WHOA! dinosore isn’t a word!  Bad human!  Learn to spell!

To solve this problem:  Download the following text file to your program directory.  This file is a dictionary listing ALL THE WORDS IN THE ENGLISH LANGUAGE:  largedictionary.txt

This file contains exactly 202,412 words (you may use this number to decide how big to make your array, if you like). A very smart approach is to create a smaller version of the dictionary (say 10 words) for testing to get things working first.

Suggestion:  Break the problem down into steps.  For example, you might implement the following “pseudo code”:

1)Create a giant array.  Read each word in from the dictionary file and store the word in the array.

2)Enter a loop in which you ask the user to enter a word.

a)Check if the word entered is in your giant array of words.

i)If so, congratulate the user.

ii)If not, berate the user for their flawed spelling.

Explanation / Answer

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main( )

{

int i;

int noofwords = 202412;

int count = 1;

static char array1 [202412][100];

char input[100];

FILE *fp1 = fopen("Dictionary.txt", "r");

if (fp1 == NULL)

{

printf("Error while opening the file. ");

exit(EXIT_FAILURE);

}

for (i = 0; i < noofwords; i++)

{

fgets(array1[i], 1000, fp1);

array1[i][strlen(array1[i]) - 1] = '';

}

fclose(fp1);

while (1)

{

printf(" Welcome to the spell checker! Please enter a word : ");

scanf("%s", input);

for (i = 0; i < noofwords; i++)

{

count = strcmp(input, array1[i]);

if (count == 0)break;

}

if (count == 0)

printf(" The word %s is spelled correctly!",input);

else

printf(" WHOA! %s isn’t a word!Bad human!Learn to spell!",input);

}

return(0);

}