C Program to Count the Number of Alphabets, Digits and Special Characters in a String

In this article, we will write a C program to count the number of alphabets, digits and special characters in a string.

The program takes the string from the user as input, counts the total number of alphabets, digits and special characters in the string and prints them on the screen.

Sample Input:

Enter any string: he@#112llo, wor#!ld

Sample Output:

Number of alphabets: 10
Number of Digits: 3
Number of Special Chars: 6

Here is the C program that counts the total number of alphabets, digits and special characters in a given string:

// C program to Count the Number of Alphabets,
// Digits and Special Characters in a String

#include <stdio.h>

int main() {
    
    char str[100];
    int alphabets, digits, spChars, i;
    
    printf("Enter any string: ");
    gets(str);
    
    i = 0;
    while(str[i]!='\0'){
        if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')){
            alphabets++;
        }
        else if(str[i]>='0' && str[i]<='9'){
            digits++;
        }
        else{
            spChars++;
        }
        i++;
    }
    
    printf("Number of alphabets: %d", alphabets);
    printf("\nNumber of Digits: %d", digits);
    printf("\nNumber of Special Chars: %d", spChars);
    
    return 0;
}

Output:

Enter any string: he@#112llo, wor#!ld
Number of alphabets: 10
Number of Digits: 3
Number of Special Chars: 6

Code Explanation:

Here is a brief explanation of how the above program works:

  • The program starts by declaring three variables – alphabets, digits, and spChars – to keep track of the number of alphabets, digits, and special characters in the string, respectively. It also declares an array of characters named str that can store up to 100 characters.
  • The user is prompted to enter a string using the printf() function and the gets() function is used to read the input string from the user and store it in the str array.
  • A while loop is used to iterate through each character of the string. The loop continues until it reaches the end of the string, which is indicated by the null character '\0'. Inside the loop, each character is checked for whether it is an alphabet, a digit, or a special character.
  • If the character is an alphabet, it increments the alphabets counter variable. The program checks for both uppercase and lowercase alphabets using the ASCII values of the characters. In ASCII, the uppercase alphabets range from 65 to 90, while the lowercase alphabets range from 97 to 122.
  • If the character is a digit, it increments the digits counter variable. The program checks for digits using the ASCII values of the characters. In ASCII, the digits range from 48 to 57.
  • If the character is not an alphabet or a digit, it increments the spChars counter variable.
  • After counting the number of alphabets, digits, and special characters, the program uses printf() to print out the respective counts to the console.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts