Guest User

Untitled

a guest
Jun 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define wordLimit 200
  5. #define wordLen 25
  6.  
  7. int isCap(char ch){
  8. if((ch>='A')&&(ch<'Z'))return 1;
  9. return 0;
  10. }
  11.  
  12. int isLetter(char ch){
  13. if((ch>='A')&&(ch<'Z'))return 1;
  14. if((ch>='a')&&(ch<'z'))return 1;
  15. return 0;
  16. }
  17.  
  18. int lowerCh(char ch){
  19. if(isCap(ch)==0)return ch;
  20. int dif='A'-'a';
  21. return(ch-dif);
  22.  
  23. }
  24.  
  25. void Initialise_frequency(int freq[]){
  26.     int y;
  27.     for(y=0;y<wordLimit;y++)freq[y]=0;
  28.     }
  29.  
  30. void replace_wrds(char key[],char re[],char buffer[],FILE *out,int *count){
  31. if(strcmp(key,buffer)==0)fprintf(out,"%s ",re);
  32. else fprintf(out,"%s ",buffer);
  33. }
  34.  
  35. int grabWord(FILE *in,char arr[]){
  36. char ch;
  37. int n=0;
  38. while((isLetter(ch==getc(in))==0)&&(ch!=EOF));
  39. if(ch==EOF)return 0;
  40. arr[n]=lowerCh(ch);
  41. while((isLetter(ch==getc(in))==0)&&(ch!=EOF)){
  42. if(n<wordLen){arr[n++]=lowerCh(ch);}}
  43. arr[n++]='\0';
  44. return 1;
  45. }
  46.  
  47. int Search(char key[],char words[][wordLen],int lo, int hi){
  48. int mid;
  49. while(hi>=lo){
  50. mid=(lo+hi)/2;
  51. if(strcmp(key,words[mid])==0)return mid;
  52. if(strcmp(key,words[mid])>0)lo=mid+1;
  53. else hi=mid-1;
  54. }
  55. return (-1*lo);
  56. }
  57.  
  58. void addWords(char key[],char words[][wordLen],int freq[],int end,int start){
  59. int j;
  60. for(j=start;j>=end;j--){
  61. strcpy(words[j+1],words[j]);
  62. freq[j+1]=freq[j];
  63. }
  64. strcpy(words[end],key);
  65. freq[end]=1;
  66. }
  67.  
  68. void printOutResults(char words[][wordLen],int freq[],int endpoint,int count){
  69. int j;
  70. printf("\nWords\tFrequency\n\n");
  71. for(j=0;j<=endpoint;j++)printf("%s\t%2d\n",words[j],freq[j]);
  72. printf("A total of %d replacements were done!\n",count);
  73. }
  74.  
  75. int main(){
  76. char words[wordLimit][wordLen],buffer[wordLen],key[wordLen],re[wordLen];
  77. int freq[wordLimit],numWrds=0,location;
  78. int count=0,extra;
  79. Initialise_frequency(freq);
  80. FILE *in=fopen("passage.txt","r");
  81. if(in==NULL){
  82. printf("Error handling file!\n");
  83. exit(1);
  84. }
  85. FILE *out=fopen("edited.txt","w");
  86. if(out==NULL){
  87. printf("Error handling the output file!\n");
  88. exit(2);
  89. }
  90. extra=grabWord(in,key);
  91. extra=grabWord(in,re);
  92. while(grabWord(in,buffer)!=0){
  93. replace_wrds(key,re,buffer,out,&count);
  94. location=Search(buffer,words,1,numWrds);
  95. if(location>=0)freq[location]++;
  96. else{
  97. if(numWrds<wordLimit){addWords(buffer,words,freq,-location,numWrds);numWrds++;}
  98. }
  99. }
  100. fclose(out);
  101. fclose(in);
  102. printOutResults(words,freq,numWrds,count);
  103. system("pause");
  104. return 0;
  105.  
  106. }
Add Comment
Please, Sign In to add comment