w3resource

C Programming: Count total number of alphabets, digits and special characters

C String: Exercise-7 with Solution

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

C Programming: Count total number of alphabets, digits and special characters

Sample Solution:

C Code:

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

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

int main() {
    char str[str_size]; // Declare a character array for the string
    int alp, digit, splch, i; // Declare variables for alphabets, digits, special characters, and iteration
    alp = digit = splch = i = 0; // Initialize counters to zero

    printf("\n\nCount total number of alphabets, digits, and special characters :\n"); // Display information about the task
    printf("--------------------------------------------------------------------\n");
    printf("Input the string : ");
    fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)

    /* Checks each character of the string */
    while (str[i] != '\0') {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
            alp++; // Increment the alphabet count if the character is an alphabet
        } else if (str[i] >= '0' && str[i] <= '9') {
            digit++; // Increment the digit count if the character is a digit
        } else {
            splch++; // Increment the special character count for all other characters
        }
        i++; // Move to the next character in the string
    }

    // Display the counts of alphabets, digits, and special characters in the string
    printf("Number of Alphabets in the string is : %d\n", alp);
    printf("Number of Digits in the string is : %d\n", digit);
    printf("Number of Special characters in the string is : %d\n\n", splch);
	
	return 0; // Return 0 to indicate successful execution of the program
}

Sample Output:

Count total number of alphabets, digits and special characters :                                              
--------------------------------------------------------------------                                          
Input the string : Welcome to w3resource.com                                                                  
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 

Flowchart:

Flowchart: Count total number of alphabets, digits and special characters

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to compare two strings without using string library functions.
Next: Write a program in C to copy one string to another string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/string/c-string-exercise-7.php