bugs.c
/*
* File: bugs.c
* Author: cteam066
*
* Created on October 27, 2012, 12:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_SIZE 2000000
#define BUG_SIZE
char buffer1[STRING_SIZE + 1];
char buffer2[STRING_SIZE + 1];
char bug[BUG_SIZE + 1];
int bug_len;
char * src, * src_end, *dest, *dest_end;
void debugLine(char * src, char * src_end, char *dest, char *dest_end){
int changed = 0;
do {
changed = 0;
//untill the end is hit
while( src < src_end ){
//find the next bug if any
const char * next_bug
= strstr( src
, bug
); //just copy the rest if no bugs are present
if ( !next_bug )
next_bug = src_end;
//mark that we found a bug
else
changed = 1;
//size to the next bug
int chars_to_copy = next_bug - src;
//append the characters to the next bug to dest
memcpy( dest_end
, src
, chars_to_copy
); //move the dest_end ptr
dest_end += chars_to_copy;
//move the source ptr
src = next_bug + bug_len;
}
//end the string for strstr
*dest_end = 0;
//swap dest and src
char * tmp = src;
src = dest;
dest = tmp;
//src end is dest end
src_end = dest_end;
//reset dest end
dest_end = dest;
} while ( changed );
}
/*
*
*/
int main(int argc, char** argv) {
int lines;
//read stuff
while( scanf("%d %s\n", &lines
, bug
) == 2 ) { int i = 0;
//for every line
for ( ; i < lines; ++i ){
//get the line
//set the source to the read line
src = buffer1;
src_end
= buffer1
+ strlen( buffer1
); //dest to the other buffer
dest = dest_end = buffer2;
debugLine(src, src_end, dest, dest_end);
}
}
return (EXIT_SUCCESS);
}