In this C program, we are going to learn how to count digits, spaces, special characters and alphabets?.
/*C program to count digits, spaces, special characters, alphabets in a string.*/ #include <stdio.h> int main() { char str[100]; int countDigits, countAlphabet, countSpecialChar, countSpaces; int counter; //assign all counters to zero countDigits = countAlphabet = countSpecialChar = countSpaces = 0; printf("Enter a string: "); gets(str); for (counter = 0; str[counter] != NULL; counter++) { if (str[counter] >= '0' && str[counter] <= '9') countDigits++; else if ((str[counter] >= 'A' && str[counter] <= 'Z') || (str[counter] >= 'a' && str[counter] <= 'z')) countAlphabet++; else if (str[counter] == ' ') countSpaces++; else countSpecialChar++; } printf("\nDigits: %d \nAlphabets: %d \nSpaces: %d \nSpecial Characters: %d", countDigits, countAlphabet, countSpaces, countSpecialChar); return 0; }
Output
Enter a string: This is test string, 123 @#% 98. Digits: 5 Alphabets: 16 Spaces: 6 Special Characters: 5
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output
need an explanation for this answer? contact us directly to get an explanation for this answer