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
Prerequisites
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.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> #include <stdlib.h> #define str_size 100 //Declare the maximum size of the string void main() { //Declaring the variables for the program char str[str_size]; 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("--------------------------------------------------------------------\n"); printf("Input the string : "); fgets(str, sizeof str, stdin); /* Checking each character of string and counting */ while(str[i]!='\0') { if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) { alps++; } else if(str[i]>='0' && str[i]<='9') { di++; } else { splchar++; } i++; } /* 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); } |
Output:-
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.