Introduction
In this post, I am going to write three programs in c language and they are
- Count Total Lowercase Letters in a Text File.
- Count Number of Uppercase Letters.
- C Program to Count Total Lowercase Letters, Uppercase Letters and Digits in a Text File.
Here, I am explaining the first program in detail. The other two programs are very much similar with first program. Only few changes are there.
File handling in c language is a very important concept. Because here we have to deal with files which are stored on secondary memory. We must be very careful while read-write operations in c programming.
In this c language program, we have to read a text file. After reading that text file, we have to count how many lowercase letters are there. How will we do it?
Also Read: Program in C to Replace Capital C with Capital S in a File
We will have to read a single character at a time. For this purpose, we will use fgetc() function. Once we read a character, how can we check whether that character is lowercase or uppercase? Because c language is case sensitive programming language. Means uppercase letters and lowercase letters are different for c compiler. For example, ‘A’ and ‘a’ are different for c compiler.
So again the question arises, how does the compiler know this case sensitivity? So the answer is ASCII values. Yes, ASCII (American Standard Code for Information Interchange) values for uppercase letters and lowercase letters are different.
Also Read: C Program to Remove White Spaces and Comments from a File
For example, ASCII value for ‘a’ is 97 and for ‘A’ is 65. In this program, we are also comparing the ASCII values for lowercase letters.
Before writing a C program to count the number of lowercase letters, let’s see the output of the program.
Output of this C Program
Contents of the file are
Hello, dear Friends,
Write a Program in C count the Number of Lowercase Letters.
Total number of Lowercase letters are 56
The actual contents of the file are displayed at line number 2 and 3. In our c program, we have to count the number of lowercase letters and displayed the message on line number 4.
Also Read: C Program to Copy the Contents of One File into Another File
Now, lets see the actual program which is written in c language.
C Program to Count the Number of Lowercase Letters in a Text File
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int count=0;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("Error while opening for reading");
return 0;
}
printf("Contents of the file are \n",count);
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
if((ch>=97)&&(ch<=122))
{
count++;
}
}
fclose(fp);
printf("\nTotal number of Lowercase letters are %d",count);
return 0;
}
Detailed Explanation of this C program
This is a very good example of file handling program in c language. Let’s see the detailed explanation for this.
FILE *fp;
For performing any operation on files in c language, we need to use this declaration. Here, we are creating a file pointer fp of type FILE. We can open a file for reading or writing using this file pointer.
char ch;
After opening a file, we will read a file character by character. The character that we will read is stored in this variable ch. That is why we have declared it as type char. The char is the data type in c language.
int count=0;
What is the aim of our c program? We need to count the number of characters in a text file. For this purpose, I have declared this variable count of int type and initialized it to the zero. There are many c language compilers, where the default value of int variable is zero. But in some compilers, the default of the int variable is a garbage value.
fp=fopen(“abc.txt”,”r”);
The fopen() is the file handling function in c language opens a file for reading or writing. here, we are opening a file “abc.txt” for reading and this file is linked with file pointer fp.
if(fp==NULL)
We know, fp is the file pointer. When there is some problem for opening a file, then fopen() returns the NULL value and that value is stored in the file pointer fp. In this, if statement we are checking whether the value of fp is equal to NULL or not. If it is true, then we will display the error message and terminate the program, otherwise this body of if will not be executed.
printf(“Error while opening a file for reading”);
As I talked above, if the value of the file pointer fp is equal to NULL, then this printf() will display this error message that Error while opening a file for reading.
while((ch=fgetc(fp))!=EOF)
This is while loop statement. This will repeat its body until the condition is true. Which condition? In this while loop, we are evaluating two expressions:
ch=fgetc(fp)
The fgetc() function reads a single character from a file which is associated with fp1. We know, fp is a file pointer points to abc.txt file. In this statement, fgetc() is reading a single character from abc.txt and storing that character in the variable ch. After storing that character in the variable ch, now we are comparing that value with EOF i.e. End of the File. How?
ch!=EOF
I have split a single statement into two statements. So after reading the character and storing in the variable ch, we will compare it with the EOF. If ch and EOF are not equal, then and only then, the body of the while loop will be executed. Otherwise, this loop will be terminated and control will transfer to the next statement followed by while loop.
Also Read: Interview Questions On C
But this condition will be false when the complete file will be read.
if((ch>=97)&&(ch<=122))
This statement is the soul of this program in c language. Why? Because, here we are deciding whether a character which is read from a file is lowercase or uppercase.
The ASCII value of ‘a’ is 97 and that of ‘z’ is 122. In this if statement, we checking the value of ch is in the range between 97 and 122. If any character is found in this range, then we will just increment the value count by 1. Otherwise, the body of this if statement will not executed and we will read next character.
Have you observed above if statement carefully? We have used the logical AND operator(&&). Here, (ch>=97) is one expression which will return a true or false value. Similarly, (ch<=122) will also return a true or false value. We need these two conditions to be true. Why?
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
Because, when we read any lowercase letter, its ASCII value must be greater than or equal to 97 and it must be less than or equal to 122. This is the range for lowercase letters. When these both conditions become true, then logical AND makes that whole expression of if statement true and body of that if statement will be executed. But if any condition becomes false or both the conditions become false, then the whole expression becomes false and the body of if statement will not be executed.
count++;
This statement is written inside the above if statement. When the above if the statement becomes true, then the value of the count variable will be incremented by one. Because we know that we need to count only lowercase letters. That is why this statement is written inside the if statement.
printf(“\nTotal number of lower case letters are %d”,count);
This is the printf() statement written outside the while loop statement. We are at this statement, that means we have read our file completely. That is why we are here at this statement.
This printf() statement will display the value of the variable count with the message. That means this will display the total number lowercase letters in a text file.
Also Read: Perfect Number in C Programming using All Loops
I hope you have understood this c language program to count the number of lowercase letters in a text file. But, what if someone asks you to write a c language program to count the number of uppercase letters in a file? Will that c program have different logic? Absolutely not. You will just have to change only one condition in that if statement where we are checking the range for lower case letters. See the following program in C language.
C Language Program to Count the Number of Uppercase Letters in a Text File
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int count=0;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("Error while opening for reading");
return 0;
}
printf("Contents of the file are \n",count);
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
if((ch>=65)&&(ch<=90))
{
count++;
}
}
fclose(fp);
printf("\nTotal number of Uppercase letters are %d",count);
return 0;
}
Output of this C Program
Contents of the file are
Hello, dear Friends,
Write a Program in C count the Number of Lowercase Letters.
Total number of Uppercase letters are 8
Write a C Program to Count the Number of Lower, Upper Case Letters and Digits in a Given File data.txt
In this program, we will count the number of lowercase letters, uppercase letters and digits separately from the file data.txt.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int countl=0,countu=0,countd=0;
fp=fopen("data.txt","r");
if(fp==NULL)
{
printf("Error while opening for reading");
return 0;
}
printf("Contents of the file are:\n\n");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
if((ch>=65)&&(ch<=90))
{
countu++;
}
else if((ch>=97)&&(ch<=122))
{
countl++;
}
if((ch>=48)&&(ch<=57))
{
countd++;
}
}
fclose(fp);
printf("\n\nTotal number of Uppercase letters are %d",countu);
printf("\nTotal number of Lowercase letters are %d",countl);
printf("\nTotal number of Digits are %d",countd);
return 0;
}
Here, we have used three counter variables to count the respective characters. The variable countu will store the number of uppercase letters, countl will store the number of lowercase letters and countd will store total digits in a file.
Output
Contents of the file are:
Hello dear friends, how are you?
I hope you are doing well. My birth of year is 1985.
Total number of Uppercase letters are 3
Total number of Lowercase letters are 57
Total number of Digits are 4
I hope you have understood all the programs. In these C language programs, logic is the same.
Also Read: Reverse a Number in C
I have also written a lot of programs in c language. So, you can check them too.
Thank you very much for reading.