Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- */
- int checkLineForMisspelling( char *line )
- {
- char **wordsInLine = malloc( 100 * sizeof(char *) );
- char *token = strtok (line, " ");
- int index = 0;
- while (token != NULL) {
- wordsInLine[index] = token;
- index++;
- token = strtok (NULL, " ");
- }
- //read through line, if strings match, compare, else just print.
- for (int i = 0; i < index; i++) {
- if ( isInDictionary( wordsInLine[i] ) ) {
- printf( "%s", wordsInLine[i] );
- } else {
- markMisspelled( wordsInLine[i] );
- }
- // printf( "%s\n", wordsInLine[i] );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement