C FILE HANDLING PROGRAMS

Question: 4

Write a function in C named “count_length()” to read the contents of a file, count and print total length of the file?(i.e. total characters in the file?)

#include<stdio.h>
 #include<conio.h>
 void count_length()
 {
 FILE *fp;
 char ch,fname[15];
 int i=0;
 printf("Enter name of the file to read ");
 scanf("%s",fname);
 fp=fopen(fname,"r");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return;
 }
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 i++;
 }
 fclose(fp);
 printf("len of file = %d\n",i);
 getch();
 }
  
 void main()
 {
   clrscr();
   count_length();
   getch();
 } 

Question: 5

Write a function in C named “count_alpha()” to read the contents of a file, count and print total alphabets in the file?

#include<stdio.h>
 #include<conio.h>
 void count_alpha()
 {
 FILE *fp;
 char ch,fname[15];
 int a=0;
 printf("Enter name of the file to read ");
 scanf("%s",fname);
 fp=fopen(fname,"r");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return;
 }
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
 a++;
 }
 fclose(fp);
 printf("total alphabets = %d\n",a);
 }
  
 void main()
 {
   clrscr();
   count_alpha();
   getch();
 } 

Question:6

Write a function in C named “count_lower()” to read the contents of a file, count and print total lower case alphabets in the file?

#include<stdio.h>
 #include<conio.h>
 void count_lower()
 {
 FILE *fp;
 char ch,fname[15];
 int a=0;
 printf("Enter name of the file to read ");
 scanf("%s",fname);
 fp=fopen(fname,"r");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return;
 }
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 if(ch>='a' && ch<='z')
 a++;
 }
 fclose(fp);
 printf("total alphabets = %d\n",a);
 }
  
 void main()
 {
   clrscr();
   count_lower();
   getch();
 } 

Question: 7

Write a function in C named “count_upper()” to read the contents of a file, count and print total upper case alphabets in the file?

#include<stdio.h>
 #include<conio.h>
 void count_upper()
 {
 FILE *fp;
 char ch,fname[15];
 int a=0;
 printf("Enter name of the file to read ");
 scanf("%s",fname);
 fp=fopen(fname,"r");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return;
 }
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 if(ch>='A' && ch<='Z')
 a++;
 }
 fclose(fp);
 printf("total upper case alphabets = %d\n",a);
 }
  
 void main()
 {
   clrscr();
   count_upper();
   getch();
 } 

Number Of Visitors

0270408