In this Programming tutorial, we will learn how to write a script in C++ and Python that can count the number of alphabets, digits, and spaces present in a text file. To write such a script we should know how to perform file handling in C++ and Python.
With the help of file handling, we can read the data from a text file using the programming languages, and based on the data we can tell the number of alphabets, digits, and spaces present in the file.
When we read data from a text file using file handling we generally read the data in string or character format. So in order to categorize whether the character or string is an alphabet or a digit we can use its ASCII value.
Example
The ASCII value of numbers starts from 48 to 57, which represents all numbers 0 to 9. For UpperCase Alphabets, the ASCII code starts from 41 up to 90. And for LowerCase alphabets the ASCII code starts from 97 up to 122.
C Program to Count no. of alphabets, digits, and spaces present in a file
Output
C++ Program to Count no. of alphabets, digits, and spaces present in a file
Output
Python
Output
Wrapping Up!
In this programming tutorial, we learned how to use ASCII code values to count the number of alphabets, spaces, and digits present in a text file. We could also have used the inbuilt string methods provided by the programing language to check if the character is an alphabet or digit, but it's good to know how to analyze a character without using any inbuilt methods. Because in interviews you might have restricted to use of any inbuilt function.
People are also reading:
- Rearrange an array in maximum minimum form
- Find whether an array is subset of another array
- Longest subarray with sum as K
- Merge Two Sorted Arrays in-place
- Print all subarrays with 0 sum
- Write a Program to convert given inches into equivalent yard, and feet
- Program to Find LCM and HCF of two numbers
- Move all zeros present in an array to the end
- Longest Palindromic Subsequence using Dynamic Programming
- Write a C++ Program to print a Man using Graphics
Leave a Comment on this Post