C File Handling Programs

Question: 19
Write a C program to read a file word by word. Further count and display words ending with either ‘t’ or ‘T’.
[Using the above program we can count and display words ending with any character.]

#include<stdio.h>
 #include<conio.h>
 #include<string.h>
 int main()
 {
    FILE *fp;
    char n[30],fname[20];
    int i=0,len;
    printf("Enter filename to read ");
    scanf("%s",fname);
    fp = fopen(fname, "r");
    //fp = fopen("test10.txt", "r");
    if(fp==NULL)
    {
      printf("Unable to open the file\n");
      getch();
      return(0);
      //exit(1); //header file: stdlib.h
    }
    while(1)
    {
    fscanf(fp, "%s", n); //read  word by word
    if(feof(fp))
    {
      printf("end of file");
      break;
    }
    len=strlen(n);
    if(n[len-1]=='t' || n[len-1]=='T')
    {
    	i=i+1;
      printf("%d %s\n",i,n);
      getch();
      //i++;
    }
    
    }
    fclose(fp);
    printf("\ntotal words = %d\n",i);
    getch();
    return(0);
 } 

Question:20
Write a C program to read a file word by word. Further count and display words with length 4.
[Using the above program we can count and display words of any length.]

#include<stdio.h>
#include<conio.h>
#include<string.h>
int  main()
 {
    FILE *fp;
    char n[30],fname[20];
    int i=0,len;
    printf("Enter filename to read ");
    scanf("%s",fname);
    fp = fopen(fname, "r");
    //fp = fopen("test10.txt", "r");
    if(fp==NULL)
    {
      printf("Unable to open the file\n");
      getch();
      return(0);
      //exit(1); //header file: stdlib.h
    }
    while(1)
    {
    fscanf(fp, "%s", n); //read  word by word
    if(feof(fp))
    {
      printf("end of file");
      break;
    }
    len=strlen(n);
    if(len==4)
    {
      printf("%s\n",n);
      i++;
    }
    
    }
    fclose(fp);
    printf("total words = %d\n",i);
    getch();
    return(0);
 } 

Number Of Visitors

0270421