C program to find the total number of alphabets, digits or special characters in a string – In this article, we will brief in on the multiple methods to find the total number of alphabets, digits or special characters is a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The methods used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using Pointers and While Loop
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
Firstly, you need to enter a particular string of your own choice, as per the given example above.
The string that is entered here is as follows:
“hello1234!@#$%”
As per the title of the blog, we need to find out the number of alphabets, digits and special characters given in the string.
Upon observation, it is seen that:
- Alphabets: 4
- Digits: 4
- Special Characters: 5
Thus, the means used to find out so in C programming are as follows:
Using Standard Method
- The ASCII values range of A to Z is 65 to 90, and a to z is 97 to 122 and 0 t0 9 is 48 to 57.
2) Read the entered string and initialize to s.
3) Iterate through string using for loop with the structure for(i=0;s[i];i++)
If the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122 then increase the alphabets count.
If the ASCII value of s[i] is in the range between 48 to 57 then increase the digits count.
If the ASCII value of s[i] is not in the range of the above two conditions then increase the special characters count.
4) Print the alphabets count, digits count and special characters count.
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 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i,alphabets=0,digits=0,specialcharacters=0; printf("Enter the string : "); gets(s); for(i=0;s[i];i++) { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) ) alphabets++; else if(s[i]>=48 && s[i]<=57) digits++; else specialcharacters++; } printf("Alphabets = %d\n",alphabets); printf("Digits = %d\n",digits); printf("Special characters = %d", specialcharacters); return 0; } |
Output:
1 2 3 4 |
Enter the string : hello1234!@#$% Alphabets = 5 Digits = 4 Special characters = 5 |
Using Function
- The main() calls the stringcount() function by passing the character array(string) as an argument.
2) The function stringcount() counts the number of alphabets, digits and special characters present in the given string as follows
for loop iterates through the string until s[i] becomes null.
a) Increase the alphabet count if the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122.
b) Increase the digits count if the ASCII value of s[i] is in the range between 48 to 57.
c) If s[i] is not in range of the above two conditions then increase the special characters count.
3) After all iterations of for loop the function prints the values of alphabets count, digits count and special characters count.
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 |
#include <stdio.h> #include <string.h> void stringcount(char *s) { int i,alphabets=0,digits=0,specialcharacters=0; for(i=0;s[i];i++) { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) ) alphabets++; else if(s[i]>=48 && s[i]<=57) digits++; else specialcharacters++; } printf("Alphabets = %d\n",alphabets); printf("Digits = %d\n",digits); printf("Special characters = %d", specialcharacters); } int main() { char s[1000]; printf("Enter the string: "); gets(s); stringcount(s); } |
Output:
1 2 3 4 |
Enter the string: qwerty12345!@#$% Alphabets = 6 Digits = 5 Special characters = 5 |
Using Recursion
- The main() calls the stringcount() function, passing the character array(string) as an argument.
2) The function stringcount()
a) if there is no character is available in s[i] i.e s[i] is null then it prints the alphabets count, digits count and special characters count values.
b) If the string contains any characters then,
b.1) If the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122 then increase the alphabets count by 1.
b.2) If the ASCII value of s[i] is in the range between 48 to 57 then digits count will be increased by 1.
b.3) If the ASCII value of s[i] is not in the range of alphabets and digits then increase the special characters count.
b.4) Increase the i value by 1, the function calls itself recursively until s[i] becomes null.
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 39 |
#include <stdio.h> #include <string.h> void stringcount(char *s) { static int i,alphabets=0,digits=0,specialcharacters=0; if(!s[i]) { printf("Alphabets = %d\n",alphabets); printf("Digits = %d\n",digits); printf("Special characters = %d", specialcharacters); return; } else { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) ) alphabets++; else if(s[i]>=48 && s[i]<=57) digits++; else specialcharacters++; i++; stringcount(s); } } int main() { char s[1000]; printf("Enter the string: "); gets(s); stringcount(s); } |
Output:
1 2 3 4 |
Enter the string: DOB 15/11/1993 Alphabets = 3 Digits = 8 Special characters = 3 |
Using Pointers And While Loop
- Here p is the pointer variable which points the string s.
a) If the first character of the string is null then exit from the while loop i.e while(*p).
b) Otherwise, it checks the ASCII value of the first character. If it is in the range of alphabets then count value of alphabets count will be increased by 1. If the ASCII value is in the range of digits then increase the digits count by 1. If the ASCII value is not in the range of the alphabets and digits then increase the special characters count by 1.
c) Increase the p-value by 1, then p points the next character of the string.
Repeat these three steps a,b,c until there is no character is available at p[i]. Then print the alphabets count, digits count and special characters count.
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 |
#include <stdio.h> #include <string.h> int main() { char s[1000],*p; int alphabets=0,digits=0,specialcharacters=0; printf("Enter the string: "); gets(s); p=s; while(*p) { if( (*p>=65 && *p<=90) || (*p>=97 && *p<=122 ) ) alphabets++; else if(*p>=48 && *p<=57) digits++; else specialcharacters++; p++; } printf("Alphabets = %d\n",alphabets); printf("Digits = %d\n",digits); printf("Special characters = %d", specialcharacters); } |
Output:
1 2 3 4 |
Enter the string: hello world 1234 Alphabets = 10 Digits = 4 Special characters = 2 |