C program to count the number of lower upper case letters and digits in a given file named data.txt

 In this I tell how to write a C program that count number of letters and digits from a given file. C program to display text from file on output. So, without wasting our time let's get started .

Code:

#include<stdio.h>
#include<stdlib.h>

int main(){
  FILE *ptr;//this we make to point towards file 
  ptr=fopen("data.txt","r");//we are opening the file  in reading mode 
  char ch;//to store char variable 
  int int_count=0;      //we need to initilize counter from zero 
  int letter_count=0;   //we need to initilize counter from zero 
  while (1)
  {
  ch=fgetc(ptr);
  if((ch>=65 && ch<=90) || (ch>=97 && ch<=122)){
      letter_count++;
  }
  else if(ch>=48 && ch<=57){
      int_count++;
  }
  else if(ch==EOF){
      break;
  }
  }
  printf("Number of letters :  %d\n",letter_count);
  printf("Number of Integers :  %d\n",int_count);
  fclose(ptr);//this will close file  


    return 0;
}

Screenshot of Text file:

                      Here we write alphabets and Integers in Text file

Logic:

for our program we need to create a text file . Here I create file name data.txt. Here we first make pointer which points towards file ptr.  And a variable ch of type character so that it can get character from file in it .And we also declare two variable int_count and letter_count and initialize both form 0 And then we open a file using Fopen  in reading mode . And then we run a infinite loop and control only come out form it only when ch is equal to EOF(end of file) and break take him out of loop  . And we give condition for it in last else if. Now let's talk about other two if condition . In first if we check whether ch comes between or equal(65 to 90) is Ascii value for small alphabet .Then we use || (or) and give Ascii value for big alphabet ( 97 to 122). And if condition comes true then increase value of letter_cout  by one . And if not then it comes in else if and we check ch  for Integers using their Ascii value(48 to 57). And this condition is true then increase value of int_count by one. Then we print using printf number of letters and number of integers . Then we release our buffer that is reversed by file using fclose.

Output:


  Here you can see the output that it give number of letters 40 and number of integers 6 . And we get our desired output . Try this program by self and please provide me feedback by commenting your views. I hope you find this helpful  in your  work . 
                                                   Thanks  for reading