C Programming: Count total number of alphabets, digits and special characters
Write a program in C to count the total number of alphabets, digits and special characters in a string.
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:
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics