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:
Screenshot of Text file:
Here we write alphabets and Integers in Text fileLogic:
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 .
No comments:
Post a Comment