Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The spellcheck.c component will contain the main() function and any other supporting functions you decide to use.
- It will be responsible for handling the command-line arguments, finding words in the text, checking to see if they're in the dictionary, and responding to user commands.
- Other than main(), you're not required to define any specific functions in spellcheck.c, but you must decide on at least two additional functions you'd like to use to simplify your implementation. You can break the problem down however you'd like, but you must use at least two non-trivial functions in addition to main. You can use more if you want.*/
- #include <stdio.h>
- #include <stdbool.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include "text.h"
- char **dictionary;
- int dictionarySize;
- char **wordLines;
- /**
- * Function to check if a word is in the dictionary. Can be
- * easily modded to take in a dictionary. Will loop through
- * the dictionary until it finds a word or until the given word
- * is smaller than current position - meaning it wasn't found.
- * @param word a char array to check if in dictionary.
- * @return 1 or 0 if found or not respectively.
- */
- int isInDictionary( char *word )
- {
- for ( int i = 0; i < dictionarySize; i++ ) {
- if ( strcmp( word, dictionary[i] ) == 0 ) {
- return 1;
- } else if ( strcmp( word, dictionary[i] ) < 0 ) {
- break;
- }
- }
- return 0;
- }
- /**
- * Function to check spelling of a given line.
- * @param
- */
- void sentenceArray( char *line )
- {
- int capacity = 1;
- char *word = malloc( capacity );
- int index = 0;
- for ( int i = 0; i < strlen( line ); i++ ) {
- if ( !isalpha( line[i] ) ) {
- index = 0;
- //call the correction method
- for ( int j = 0; j < capacity; j++ ) {
- word[j] = '\0';
- }
- } else {
- word[index] = line[i];
- word[index + 1] = '\0';
- index++;
- if ( index == capacity ) {
- capacity *= 2;
- word = (char *)realloc(word, capacity);
- }
- }
- }
- }
- /**
- * qsort comparing
- */
- int compareStrings( const void* a, const void* b )
- {
- const char *str1 = *(const char **)a;
- const char *str2 = *(const char **)b;
- int str1Len = strlen( str1 );
- int str2Len = strlen( str2 );
- if ( str1Len >= str2Len ) {
- return strncmp(str1, str2, str1Len);
- } else {
- return strncmp(str1, str2, str2Len);
- }
- }
- void spellcheckLoop()
- {
- char input[5];
- }
- /**
- * Program Start.
- */
- int main( int argc, char *argv[] )
- {
- // Check command-line arguments and open the input file.
- FILE *fp = fopen( argv[ 1 ], "r" );
- FILE *dictionaryFile = fopen( "words.txt", "r" );;
- if ( fp == NULL ) {
- printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
- exit( EXIT_FAILURE );
- }
- if ( argc == 3 ) {
- fclose( dictionaryFile );
- dictionaryFile = fopen( argv[ 2 ], "r" );
- }
- fclose( fp );
- fclose( dictionaryFile );
- if ( argc > 3 || argc < 2 ) {
- printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
- exit( EXIT_FAILURE );
- }
- //Words from text files.
- int linesSize = 0;
- int *linePointer = &linesSize;
- wordLines = readLines( argv[ 1 ], linePointer );
- //Dictionary.
- dictionarySize = 0;
- int *countPointer = &dictionarySize;
- if ( argc == 2 ) {
- dictionary = readLines( "words.txt", countPointer );
- } else {
- dictionary = readLines( argv[ 2 ], countPointer );
- }
- qsort( dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
- spellcheckLoop();
- freeLines( dictionary, dictionarySize );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement