Tech Study

C++: Count Alphabets Digits Special Character In String

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#include <string.h>
 
#define MAX_SIZE 100 //Maximum size of the string
using namespace std;
 
int main()
{
    char string[MAX_SIZE];
    int alphabets, digits, specialchars, i;
 
    alphabets = digits = specialchars = i = 0;
 
    cout<<"Enter any string: ";
    cin>>string;
 
     while(string[i]!='\0')
    {
        if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))
        {
            alphabets++;
        }
        else if(string[i]>='0' && string[i]<='9')
        {
            digits++;
        }
        else
        {
            specialchars++;
        }
 
        i++;
    }
 
    cout<<"Total Alphabets: "<<alphabets<<endl;
    cout<<"Total Digits: "<<digits<<endl;
    cout<<"Total Special characters: "<<specialchars<<endl;
 
}
 
 

Result

Write C++ program to count number of alphabets, digits and special characters in string
Write C++ program to count number of alphabets, digits and special characters in string

C Program to Count Number of Notes

Sum of Even Numbers in C till N

Taggeddigits and special characters in stringWrite C++ program to count number of alphabets

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more