Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void replace (char *, char *, char *);
- int main(int argc, char *argv[]){
- FILE *f = fopen("Input.txt", "r");
- FILE *output =fopen("Dante.txt", "w");
- char str[500];
- //alerts user if file doesn't exist
- if (f == NULL){
- printf("File not exist.\n");
- exit(0);
- }
- //Changing the words to the new words in the file
- while(fgets(str, 500, f)){
- for(int i = 0; i < 500; i++){
- if(strstr(str, "Inferno") != NULL){
- replace(str, "Inferno", "Paradiso");
- }
- if(strstr(str, "In dark woods") != NULL){
- replace(str, "In dark woods", "using Windows 8.1");
- }
- if(strstr(str, "those woods") != NULL){
- replace(str, "those woods", "Windows 8.1");
- }
- if(strstr(str, "to enter") != NULL){
- replace(str, "to enter", "to use 8.1");
- }
- if(strstr(str, "crest") != NULL){
- replace(str, "crest", "screen");
- }
- if(strstr(str, "Below a hill") != NULL){
- replace(str, "Below a hill", "Before a monitor");
- }
- if(strstr(str, "shoulders") != NULL){
- replace(str, "shoulders", "GUI");
- }
- if(strstr(str, "planet") != NULL){
- replace(str, "planet", "Shakespeare");
- }
- }
- fprintf(output,"%s", str);
- }
- fclose(f);
- return 0;
- }
- void replace(char * o_string, char * s_string, char * r_string) {
- //a variable to replace things
- char buffer[1000];
- //to store the pointer returned from str
- char * ch;
- //exit condition
- if(!(ch = strstr(o_string, s_string)))
- return;
- //copy all the content to buffer before the first occurrence of the search string
- strncpy(buffer, o_string, ch-o_string);
- //prepare the buffer for appending by adding a null to the end of it
- buffer[ch-o_string] = 0;
- //append using sprintf function
- sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
- //empty o_string for copying
- o_string[0] = 0;
- strcpy(o_string, buffer);
- //pass recursively to replace other occurrences
- return replace(o_string, s_string, r_string);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement