Source code for submission s1135

bugs.c

  1. /*
  2.  * File: bugs.c
  3.  * Author: cteam066
  4.  *
  5.  * Created on October 27, 2012, 12:02 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12.  
  13. #define STRING_SIZE 2000000
  14. #define BUG_SIZE
  15.  
  16. char buffer1[STRING_SIZE + 1];
  17. char buffer2[STRING_SIZE + 1];
  18. char bug[BUG_SIZE + 1];
  19. int bug_len;
  20. char * src, * src_end, *dest, *dest_end;
  21.  
  22. void debugLine(char * src, char * src_end, char *dest, char *dest_end){
  23. int changed = 0;
  24.  
  25. do {
  26. changed = 0;
  27. //untill the end is hit
  28. while( src < src_end ){
  29. //find the next bug if any
  30. const char * next_bug = strstr( src, bug );
  31. //just copy the rest if no bugs are present
  32. if ( !next_bug )
  33. next_bug = src_end;
  34. //mark that we found a bug
  35. else
  36. changed = 1;
  37. //size to the next bug
  38. int chars_to_copy = next_bug - src;
  39. //append the characters to the next bug to dest
  40. memcpy( dest_end, src, chars_to_copy );
  41. //move the dest_end ptr
  42. dest_end += chars_to_copy;
  43. //move the source ptr
  44. src = next_bug + bug_len;
  45. }
  46. //end the string for strstr
  47. *dest_end = 0;
  48.  
  49. //swap dest and src
  50. char * tmp = src;
  51. src = dest;
  52. dest = tmp;
  53.  
  54. //src end is dest end
  55. src_end = dest_end;
  56. //reset dest end
  57. dest_end = dest;
  58.  
  59. } while ( changed );
  60.  
  61.  
  62.  
  63. printf("%s\n", src);
  64. }
  65.  
  66. /*
  67.  *
  68.  */
  69. int main(int argc, char** argv) {
  70. int lines;
  71.  
  72. //read stuff
  73. while( scanf("%d %s\n", &lines, bug) == 2 ) {
  74. bug_len = strlen(bug);
  75. int i = 0;
  76. //for every line
  77. for ( ; i < lines; ++i ){
  78. //get the line
  79. gets( buffer1 );
  80. //set the source to the read line
  81. src = buffer1;
  82. src_end = buffer1 + strlen( buffer1 );
  83. //dest to the other buffer
  84. dest = dest_end = buffer2;
  85. debugLine(src, src_end, dest, dest_end);
  86. }
  87. }
  88.  
  89.  
  90. return (EXIT_SUCCESS);
  91. }
  92.  
  93.