Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void printRemaining( char *line, int start, char *word )
- {
- // printf( "\n%d, %d, %d\n", (int)start, start+(int)strlen(word), (int)strlen(line) );
- for ( int i = start + strlen( word ); i < strlen( line ); i++ ){
- printf( "%c", line[i] );
- }
- }
- /**
- * Prints the lines above and below the line containing the word not in dictionary.
- * Finds the incorrect word and higlights it red.
- */
- void printNearbyLines( char *word, int index, int textSize )
- {
- printf("\n"); // always helpful. Probably needed.
- int actualLength = LOCAL_LINES_MAX; // used for edges.
- char **nearest = malloc( LOCAL_LINES_MAX * sizeof( char * ) );
- for ( int i = 0; i < LOCAL_LINES_MAX; i++ ) {
- nearest[i] = ( char * ) malloc( INPUT_SIZE * sizeof ( char ) );
- }
- if ( index == 0 ) { // Word on top edge
- strcpy( nearest[0], wordLines[index] );
- strcpy( nearest[1], wordLines[index + 1] );
- actualLength--;
- } else if ( index == textSize - 1 ) { // Word on bottom edge
- strcpy( nearest[1], wordLines[index - 1] );
- strcpy( nearest[2], wordLines[index] );
- actualLength--;
- } else { // General case: word not on edge.
- strcpy( nearest[0], wordLines[index - 1] );
- strcpy( nearest[1], wordLines[index] );
- strcpy( nearest[2], wordLines[index + 1] );
- }
- // Now we check for that word.
- for ( int i = 0; i < actualLength; i++ ) {
- if ( strstr( nearest[i], word ) != NULL ) { // checking bigger string.
- char *lineBackup = calloc( strlen( nearest[i] ), sizeof( char ) );
- strcpy( lineBackup, nearest[i] );
- // printf( "\n%d\n", (int)strlen( lineBackup ) );
- char *token = strtok(nearest[i], " ");
- while ( token != NULL ) {
- if ( strstr( token, word ) != NULL ) { // Obama to public: We got 'im.
- markMisspelled( token );
- int pos = token - nearest[i];
- printRemaining( lineBackup, pos, token );
- break;
- } else { // we got the wrong guy... er, word, so let's just print it.
- printf("%s ", token);
- }
- token = strtok( NULL, " " ); // seriously this threw me off so bad.
- }
- free( lineBackup );
- printf("\n");
- } else {
- printf( "%s\n", nearest[i] );
- }
- }
- freeLines( nearest, LOCAL_LINES_MAX );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement