Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define wordLimit 200
- #define wordLen 25
- int isCap(char ch){
- if((ch>='A')&&(ch<'Z'))return 1;
- return 0;
- }
- int isLetter(char ch){
- if((ch>='A')&&(ch<'Z'))return 1;
- if((ch>='a')&&(ch<'z'))return 1;
- return 0;
- }
- int lowerCh(char ch){
- if(isCap(ch)==0)return ch;
- int dif='A'-'a';
- return(ch-dif);
- }
- void Initialise_frequency(int freq[]){
- int y;
- for(y=0;y<wordLimit;y++)freq[y]=0;
- }
- void replace_wrds(char key[],char re[],char buffer[],FILE *out,int *count){
- if(strcmp(key,buffer)==0)fprintf(out,"%s ",re);
- else fprintf(out,"%s ",buffer);
- }
- int grabWord(FILE *in,char arr[]){
- char ch;
- int n=0;
- while((isLetter(ch==getc(in))==0)&&(ch!=EOF));
- if(ch==EOF)return 0;
- arr[n]=lowerCh(ch);
- while((isLetter(ch==getc(in))==0)&&(ch!=EOF)){
- if(n<wordLen){arr[n++]=lowerCh(ch);}}
- arr[n++]='\0';
- return 1;
- }
- int Search(char key[],char words[][wordLen],int lo, int hi){
- int mid;
- while(hi>=lo){
- mid=(lo+hi)/2;
- if(strcmp(key,words[mid])==0)return mid;
- if(strcmp(key,words[mid])>0)lo=mid+1;
- else hi=mid-1;
- }
- return (-1*lo);
- }
- void addWords(char key[],char words[][wordLen],int freq[],int end,int start){
- int j;
- for(j=start;j>=end;j--){
- strcpy(words[j+1],words[j]);
- freq[j+1]=freq[j];
- }
- strcpy(words[end],key);
- freq[end]=1;
- }
- void printOutResults(char words[][wordLen],int freq[],int endpoint,int count){
- int j;
- printf("\nWords\tFrequency\n\n");
- for(j=0;j<=endpoint;j++)printf("%s\t%2d\n",words[j],freq[j]);
- printf("A total of %d replacements were done!\n",count);
- }
- int main(){
- char words[wordLimit][wordLen],buffer[wordLen],key[wordLen],re[wordLen];
- int freq[wordLimit],numWrds=0,location;
- int count=0,extra;
- Initialise_frequency(freq);
- FILE *in=fopen("passage.txt","r");
- if(in==NULL){
- printf("Error handling file!\n");
- exit(1);
- }
- FILE *out=fopen("edited.txt","w");
- if(out==NULL){
- printf("Error handling the output file!\n");
- exit(2);
- }
- extra=grabWord(in,key);
- extra=grabWord(in,re);
- while(grabWord(in,buffer)!=0){
- replace_wrds(key,re,buffer,out,&count);
- location=Search(buffer,words,1,numWrds);
- if(location>=0)freq[location]++;
- else{
- if(numWrds<wordLimit){addWords(buffer,words,freq,-location,numWrds);numWrds++;}
- }
- }
- fclose(out);
- fclose(in);
- printOutResults(words,freq,numWrds,count);
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment