
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
- Related Articles
- Finding count of special characters in a string in JavaScript
- Finding and returning uncommon characters between two strings in JavaScript
- Find all strings formed from characters mapped to digits of a number in Python
- C++ code to find total number of digits in special numbers
- Finding product of Number digits in JavaScript
- Converting numbers into corresponding alphabets and characters using JavaScript
- Java program to find the percentage of uppercase, lowercase, digits and special characters in a String
- Strings in C Language
- Finding sum of first and last digit using divide and modulo operator in C language
- Finding even and odd numbers in a set of elements dynamically using C language
- How to separate strings in R that are joined with special characters?
- Finding only strings beginning with a number using MySQL Regex?
- Finding the 1-based index of a character in alphabets using JavaScript
- Finding sum of digits of a number until sum becomes single digit in C++
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++

Advertisements