Starting Vocational Training From 1-May-2024 Get Detail


Write a program in C to count total number of alphabets, digits and special characters in a string. 

Test Data :
Input the string : Welcome to w3resource.com

Expected Output :

Number of Alphabets in the string is : 21 
Number of Digits in the string is : 1 
Number of Special characters in the string is : 4 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define str_size 100 //Declare the maximum size of the string

void main()
{
    char str[str_size];
    int alp, digit, splch, i;
    alp = digit = splch = i = 0;


       printf(" Count total number of alphabets, digits and special characters : ");
       printf("-------------------------------------------------------------------- ");     
       printf("Input the string : ");
       fgets(str, sizeof str, stdin);    

     /* Checks each character of string*/

    while(str[i]!=)
    {
        if((str[i]>=a && str[i]<=z) || (str[i]>=A && str[i]<=Z))
        {
            alp++;
        }
        else if(str[i]>=0 && str[i]<=9)
        {
            digit++;
        }
        else
        {
            splch++;
        }

        i++;
    }

    printf("Number of Alphabets in the string is : %d ", alp);
    printf("Number of Digits in the string is : %d ", digit);
    printf("Number of Special characters in the string is : %d ", splch);
}