Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding number of alphabets, digits and special characters in strings using C language
Following is the logic we implement to find alphabets, digits and special characters −
for(number=0;string[number]!='\0';number++) {// for loop until endof string
if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z') //checking alphabets in string{
alphabets=alphabets+1; //counting alphabets
//alphabets++;
}
else if(string[number]>='0'&&string[number]<='9'){ //checking numbers in string
digits=digits+1; //counting numbers
//digits++;
} else {
special=special+1; //counting special characters
//special++;
}
}
Following program is to identify total number of alphabets, digits, and special characters in a string −
Example
#include<stdio.h>
#include<ctype.h>
void main(){
//Declaring integer for number determination, string//
int number;
char string[50];
int alphabets=0;
int digits=0;
int special=0;
//Reading User I/p//
printf("Enter the string :");
gets(string);
for(number=0;string[number]!='\0';number++){
if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z'){
alphabets=alphabets+1;
//alphabets++;
}
else if(string[number]>='0'&&string[number]<='9'){
digits=digits+1;
//digits++;
}
else{
special=special+1;
//special++;
}
}
//Printing number of alphabets, number of digits, number of special characters//
printf("The number of alphabets in the string is : %d
",alphabets);
printf("The number of digits in the string is : %d
",digits);
printf("The number of special characters in the string is : %d
",special);
}
Output
Enter the string :The number of alphabets in the string is : 0 The number of digits in the string is : 0 The number of special characters in the string is : 1
Advertisements