In this tutorial you will learn about the C Program to Count Alphabets, Digits and Special Characters in a String and its application with practical example.
C Program to Count Alphabets, Digits, and Special Characters in a String
In this tutorial, we will learn to create a C program that will Count Alphabets, Digits, and Special Characters in a String in C programming
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in c programming.
What is a string?
The String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the typeable data, it means Digits, Alphabets, Symbols, etc. A character is any single entity that is used in combination to form a word.
1. Declaring the variables for the program.
2. Taking the input number from the user.
3. Adding the given sting to program.
4. Passing those variables to for loop.
5. Using those conditions to count and filter them with for loop.
6. Printing the result words.
7. End Program.
Count Alphabets, Digits, and Special Characters in a String
First, will take the line of string from the user. Then will count Alphabets, Digits, and Special Characters from that strings. At last, we will print the result numbers.
With the help of this program, we can Count Alphabets, Digits, and Special Characters in a String.
#define str_size 100 //Declare the maximum size of the string
//Declaring the variables for the program
int alps, di, splchar, i;
alps = di = splchar = i = 0;
//Taking the input string from the user for the counting
printf("\n\nCount total number of alphabets, ditis and special characters :\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
/* Checking each character of string and counting */
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
else if(str[i]>='0' && str[i]<='9')
/* Printing the ooutput countings */
printf("Number of alphabets in the string is : %d\n", alps);
printf("Number of ditis in the string is : %d\n", di);
printf("Number of Special characters in the string is : %d\n\n", splchar);
In the above program, we have first initialized the required variable.
- str[str_size] = it will hold the string value.
- apls= it will hold the integer value for vowels.
- di = it will hold the integer value for digits.
- splchar = it will hold the integer value for special characters.
- i = it will hold the integer value.
Input strings from the user.
Filtering the digits and the special character from the string.
Printing output numbers of the alphabets, the special characters, and the digits.