Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // the function that explains the basics of the program
- void help()
- {
- printf("\nHELP:\nYou have either opened this page by asking for help or by giving wrong inputs.\n");
- printf("\nThe proper way of starting the program is as follows:\n\n");
- printf("\n<program.exe> <input_text.txt> <output_text.txt> <old_word> <new_word> \n\n");
- printf("EXPLANATION\n");
- printf("\n<program.exe> - the path to the program.\n");
- printf("<input_text.txt> - the path to the text you wish to work on.\n");
- printf("<output_text.txt> - the path to the new text with replaced word.\n");
- printf("<old_word> - the word in the <input_text.txt> you wish to change.\n");
- printf("<new_word> - the word that is going to replace the <old_word>. \n");
- }
- //the function that changes the words given by the user
- void changer()
- {
- char word_to_change[100];
- char new_word[100];
- char character[1];
- char present_word[100]=" ";
- int i=0;
- FILE *original;
- FILE *changed; //opening the files
- original=fopen("TestFile.txt","r");
- changed=fopen("TestFileChanged.txt","w"); //loading files
- scanf("%s", word_to_change);
- printf("\n");
- scanf("%s", new_word);
- if(!original){printf("Your input file doesn't exist"); return;}
- if(!changed){printf("Your output file doesn't exist"); return;} //checking wether the files exist
- while(!feof(original)) //the loop checks if there is any data inside the input file
- {
- character[0]=getc(original); //checking of the input file one character at a time
- if(character[0]!=' '&&character[0]!='.'&&character[0]!=','&&character[0]!='\n'&&character[0]!='\t') //ignoring the punctuation and other special cases
- {
- present_word[i]=character[0]; //this loop creates a word we will later check
- i++;
- }
- else // if the else loop starts it means that the word ended and we encountered a character different than a letter
- {
- if(strcmp(word_to_change,present_word)==0) fprintf(changed,"%s", new_word);
- else fprintf(changed,"%s",present_word); //we print the letter to the file and follow it with a character that made the if statement false
- fprintf(changed,"%c",character[0]);
- while(i!=0)
- {
- present_word[i-1]='\0'; // we have to reset the counter and empty the word container
- i--;
- }
- }
- }// NOTE: SOMETHING IS NOT RIGHT, THE OUTPUT FILE HAS AN EXTRA SIGN AT THE END, ALSO IT DOESN'T CHANGE THE LAST WORD IF IT DOESN'T END WITH A SPECIAL SIGN(???) (USED TO WORK BEFORE FPRINT WAS USED)
- if(strcmp(word_to_change,present_word)==0){ printf("%s", new_word);}
- else {printf("%s",present_word);} //this line let's us print out the last word which would get lost other otherwise (in case the input file ends without a special sign, if it
- // end with, for example dot, the string is cleared co this command has no effect and everything is allright_
- //^^not anymore
- fclose(original); //closing of files
- fclose(changed);
- return;
- }
- int main()
- {
- // help();
- changer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement