Advertisement
Guest User

changer

a guest
Nov 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // the function that explains the basics of the program
  6. void help()
  7. {
  8. printf("\nHELP:\nYou have either opened this page by asking for help or by giving wrong inputs.\n");
  9. printf("\nThe proper way of starting the program is as follows:\n\n");
  10. printf("\n<program.exe> <input_text.txt> <output_text.txt> <old_word> <new_word> \n\n");
  11. printf("EXPLANATION\n");
  12. printf("\n<program.exe> - the path to the program.\n");
  13. printf("<input_text.txt> - the path to the text you wish to work on.\n");
  14. printf("<output_text.txt> - the path to the new text with replaced word.\n");
  15. printf("<old_word> - the word in the <input_text.txt> you wish to change.\n");
  16. printf("<new_word> - the word that is going to replace the <old_word>. \n");
  17.  
  18. }
  19. //the function that changes the words given by the user
  20. void changer()
  21. {
  22. char word_to_change[100];
  23. char new_word[100];
  24. char character[1];
  25. char present_word[100]=" ";
  26. int i=0;
  27.  
  28. FILE *original;
  29. FILE *changed; //opening the files
  30.  
  31. original=fopen("TestFile.txt","r");
  32. changed=fopen("TestFileChanged.txt","w"); //loading files
  33.  
  34. scanf("%s", word_to_change);
  35. printf("\n");
  36. scanf("%s", new_word);
  37.  
  38. if(!original){printf("Your input file doesn't exist"); return;}
  39. if(!changed){printf("Your output file doesn't exist"); return;} //checking wether the files exist
  40.  
  41. while(!feof(original)) //the loop checks if there is any data inside the input file
  42. {
  43. character[0]=getc(original); //checking of the input file one character at a time
  44. if(character[0]!=' '&&character[0]!='.'&&character[0]!=','&&character[0]!='\n'&&character[0]!='\t') //ignoring the punctuation and other special cases
  45. {
  46. present_word[i]=character[0]; //this loop creates a word we will later check
  47. i++;
  48. }
  49. else // if the else loop starts it means that the word ended and we encountered a character different than a letter
  50. {
  51. if(strcmp(word_to_change,present_word)==0) fprintf(changed,"%s", new_word);
  52. 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
  53. fprintf(changed,"%c",character[0]);
  54. while(i!=0)
  55. {
  56. present_word[i-1]='\0'; // we have to reset the counter and empty the word container
  57. i--;
  58. }
  59. }
  60. }// 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)
  61. if(strcmp(word_to_change,present_word)==0){ printf("%s", new_word);}
  62. 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
  63. // end with, for example dot, the string is cleared co this command has no effect and everything is allright_
  64. //^^not anymore
  65. fclose(original); //closing of files
  66. fclose(changed);
  67. return;
  68. }
  69.  
  70. int main()
  71. {
  72. // help();
  73. changer();
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement