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"
- #define INPUT_SIZE 100
- char **dictionary;
- int dictionarySize;
- char **wordLines;
- int linesSize;
- /**
- * 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);
- }
- }
- /**
- * 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 )
- {
- //word to be compared, must be made lowercase
- //but we don't want to modify the word itself.
- char lowercaseWord[ strlen( word ) + 1 ];
- for ( int i = 0; i < strlen( word ); i++ ) {
- lowercaseWord[i] = tolower( word[i] );
- }
- lowercaseWord[ strlen( word ) ] = '\0';
- for ( int i = 0; i < dictionarySize; i++ ) {
- if ( strcmp( lowercaseWord, dictionary[i] ) == 0 ) {
- return 1;
- } else if ( strcmp( lowercaseWord, dictionary[i] ) < 0 ) {
- break;
- }
- }
- // int *value = bsearch( lowercaseWord, dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
- return 0;
- }
- /**
- * When called, markMisspelled will change color of output
- * to red. Once called again, standard color.
- */
- void markMisspelled( char *word )
- {
- printf( "\x1B[31m" );
- printf( "%s", word );
- printf( "\x1B[0m" );
- }
- /**
- * Function to check spelling of a given line.
- * @param
- */
- //Not finished
- int checkLineForMisspelling( char *line )
- {
- bool breakLine = false;
- //read through line, if strings match, compare, else just print.
- int capacity = 5;
- char *word = malloc( capacity );
- //null terminating string
- for ( int i = 0; i < capacity; i++ ) {
- line[i] = '\0';
- }
- for (int i = lineIndex; i < index; i++) {
- if ( breakLine ) {
- } else {
- if ( line[i] == '\n' ) {
- break;
- } else {
- if ( isalpha( line[i] ) ) {
- word[index] = line[i];
- index++;
- if ( index == capacity ) {
- capacity *= 2;
- line = (char *)realloc(line, capacity);
- //getting those null terminators in
- for ( int i = index; i < capacity; i++ ) {
- line[i] = '\0';
- }
- }
- } else {
- if ( word[0] != '\0' ) {
- if ( isInDictionary( word ) ) {
- printf( "%s", );
- } else {
- }
- }
- printf( "%c", line[i] );
- }
- }
- }
- }
- // for loop through each line
- // create a word from all alpha characters, otherwise print
- // print word, red if not in dictionary
- // then correct is necessary.
- // then move on through loop
- return breakLine;
- }
- /**
- * 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.
- 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 );
- }
- // Check dictionary for errors.
- for ( int i = 0; i < dictionarySize; i++ ) {
- int stringLength = strlen( dictionary[i] );
- if ( dictionary[i][0] == 0 ) {
- printf( "Invalid word, line: %d\n", i + 1 );
- return EXIT_FAILURE;
- }
- for ( int j = 0; j < stringLength; j++ ) {
- if ( !isalpha( dictionary[i][j] ) ) {
- printf( "Invalid word, line: %d\n", i + 1 );
- return EXIT_FAILURE;
- }
- if ( dictionary[i][j] != tolower( dictionary[i][j] ) ) {
- printf( "Invalid word, line: %d\n", i + 1 );
- return EXIT_FAILURE;
- }
- }
- }
- //sort.
- qsort( dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
- int index = 0;
- while ( wordLines[index] != NULL ) {
- char *input;
- input = (char *) malloc( 1 * sizeof( char ) );
- // Prompt user for input
- printf( "(r)eplace, (a)dd word, (n)ext or (q)uit: ");
- scanf( "%s", input );
- input[0] = tolower( input[0] );
- if( input[0] == 'q' ) {
- fprintf( stderr, "Discarding changes" );
- break;
- } else if ( input[0] == 'r' ) {
- printf( "Replacing.\n" );
- } else if ( input[0] == 'a' ) {
- printf( "Adding Word.\n" );
- } else if ( input[0] == 'n' ) {
- printf( "Ignoring.\n" );
- } else {
- printf( "Unknown command\n" );
- i--;
- }
- }
- //this will eventually do stuff.
- // spellcheckLoop();
- //free it all up.
- freeLines( dictionary, dictionarySize );
- //just to be safe.
- printf( "\n" );
- //oh ye and exit too
- return EXIT_SUCCESS;
- }
- /**
- * UI for user input. Prompts the user to either (r)eplace an incorrect word,
- * (a)dd a word to the dictionary, move to the (n)ext word in the text file,
- * or (q)uit the program
- char * getUserInput()
- {
- char *input;
- input = (char *) malloc( INIT_SIZE * sizeof( char ) );
- // Prompt user for input
- printf( "(r)eplace, (a)dd word, (n)ext or (q)uit: ");
- scanf( "%s", input );
- input[0] = tolower( input[0] );
- if( input[0] == 'r' ) {
- char *toReplace;
- toReplace = ( char * ) malloc( INIT_SIZE * sizeof( char ) );
- scanf( "%s", toReplace );
- // Merge the two strings
- strcat( input, toReplace );
- }
- return input;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement