How to count alphabets, numeric and special characters ?

In this example, we have to count digits, spaces, special characters and alphabets using C program.

#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: wikitechy, 789@#%12.

Digits: 5
Alphabets: 9
Spaces: 1
Special Characters: 5

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,