Advertisement
Shanix

Untitled

Mar 17th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. void printRemaining( char *line, int start, char *word )
  2. {
  3. // printf( "\n%d, %d, %d\n", (int)start, start+(int)strlen(word), (int)strlen(line) );
  4. for ( int i = start + strlen( word ); i < strlen( line ); i++ ){
  5. printf( "%c", line[i] );
  6. }
  7. }
  8.  
  9. /**
  10. * Prints the lines above and below the line containing the word not in dictionary.
  11. * Finds the incorrect word and higlights it red.
  12. */
  13. void printNearbyLines( char *word, int index, int textSize )
  14. {
  15.  
  16. printf("\n"); // always helpful. Probably needed.
  17.  
  18. int actualLength = LOCAL_LINES_MAX; // used for edges.
  19.  
  20. char **nearest = malloc( LOCAL_LINES_MAX * sizeof( char * ) );
  21. for ( int i = 0; i < LOCAL_LINES_MAX; i++ ) {
  22. nearest[i] = ( char * ) malloc( INPUT_SIZE * sizeof ( char ) );
  23. }
  24. if ( index == 0 ) { // Word on top edge
  25. strcpy( nearest[0], wordLines[index] );
  26. strcpy( nearest[1], wordLines[index + 1] );
  27. actualLength--;
  28. } else if ( index == textSize - 1 ) { // Word on bottom edge
  29. strcpy( nearest[1], wordLines[index - 1] );
  30. strcpy( nearest[2], wordLines[index] );
  31. actualLength--;
  32. } else { // General case: word not on edge.
  33. strcpy( nearest[0], wordLines[index - 1] );
  34. strcpy( nearest[1], wordLines[index] );
  35. strcpy( nearest[2], wordLines[index + 1] );
  36. }
  37.  
  38. // Now we check for that word.
  39. for ( int i = 0; i < actualLength; i++ ) {
  40. if ( strstr( nearest[i], word ) != NULL ) { // checking bigger string.
  41. char *lineBackup = calloc( strlen( nearest[i] ), sizeof( char ) );
  42. strcpy( lineBackup, nearest[i] );
  43. // printf( "\n%d\n", (int)strlen( lineBackup ) );
  44. char *token = strtok(nearest[i], " ");
  45. while ( token != NULL ) {
  46. if ( strstr( token, word ) != NULL ) { // Obama to public: We got 'im.
  47. markMisspelled( token );
  48. int pos = token - nearest[i];
  49. printRemaining( lineBackup, pos, token );
  50. break;
  51. } else { // we got the wrong guy... er, word, so let's just print it.
  52. printf("%s ", token);
  53. }
  54. token = strtok( NULL, " " ); // seriously this threw me off so bad.
  55. }
  56. free( lineBackup );
  57. printf("\n");
  58. } else {
  59. printf( "%s\n", nearest[i] );
  60. }
  61. }
  62.  
  63. freeLines( nearest, LOCAL_LINES_MAX );
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement