Pages

Friday, 9 February 2018

Write a program to carry out the following: − To read a text file “TRIAL.TXT” consisting of a maximum of 50 lines of text, each line with a maximum of 80 characters. − Count and display the number of words contained in the file. − Display the total number of four letter words in the text file. Assume that the end of a word may be a space, comma or a full-stop followed by one or more spaces or a newline character.


Write a program to carry out the following:
− To read a text file “TRIAL.TXT” consisting of a maximum of 50 lines of text, each line with a maximum of 80 characters.
− Count and display the number of words contained in the file.
− Display the total number of four letter words in the text file.
Assume that the end of a word may be a space, comma or a full-stop followed by one or more spaces or a newline character.

#include<stdio.h>

#include<conio.h>

void main() {



FILE *fp;

char s[80];

int twd,fwd,tw=0,fw=0;
void word();
clrscr();

fp=fopen("TRIAL.TXT","r");
if(fp==NULL) {

exit();
}

while(fgets(s,79,fp)!=NULL)  {
word(s,&twd,&fwd);
tw=tw+twd;
fw=fw+fwd;
}

fclose(fp);

printf("\nTotal number of words in text file = %d\n",tw);
printf("\n\nTotal number of 4 letter words = %d\n",fw);

getch();
}

void word(char ss[80],int *tw, int *fw) {

int i=0,tot_wd=0,tot_4_wd=0;

while(ss[i]!='\0') {

/************************/
/* to cound total words */
/************************/

if(ss[i]>=65 && ss[i]<=90 || ss[i]>=97 && ss[i]<=122) {

if(ss[i+1]==' ' || ss[i+1]=='.' || ss[i+1]==',' || ss[i+1]=='\n' ) {

tot_wd++;
}
}



/*********************************/
/* to count total 4 letter words */
/*********************************/

if(ss[i]==' ' || ss[i]==',' || ss[i]=='.')  {

if(ss[i+1]>=65 && ss[i+1]<=90 || ss[i+1]>=97 && ss[i+1]<=122) {

if(ss[i+2]>=65 && ss[i+2]<=90 || ss[i+2]>=97 && ss[i+2]<=122) {

if(ss[i+3]>=65 && ss[i+3]<=90 || ss[i+3]>=97 && ss[i+3]<=122) {

if(ss[i+4]>=65 && ss[i+4]<=90 || ss[i+4]>=97 && ss[i+4]<=122)  {

if(ss[i+5]==' ' || ss[i+5]==',' || ss[i+5]=='.' || ss[i+5]=='\n') {

tot_4_wd++;
     }
    }
   }
  }
 }
}

if(ss[i]>=65 && ss[i]<=90 || ss[i]>=97 && ss[i]<=122) {

if(ss[i+1]>=65 && ss[i+1]<=90 || ss[i+1]>=97 && ss[i+1]<=122) {

if(ss[i+2]>=65 && ss[i+2]<=90 || ss[i+2]>=97 && ss[i+2]<=122) {

if(ss[i+3]>=65 && ss[i+3]<=90 || ss[i+3]>=97 && ss[i+3]<=122)  {

if(ss[i+4]==' ' || ss[i+4]==',' || ss[i+4]=='.' || ss[i+4]=='\n') {

    }
   }
  }
 }
}


i++;

}

*tw=tot_wd;
*fw=tot_4_wd;

}