Find the total number of alphabets, digits, or special characters in a string with C.
#include <stdio.h>
int main()
{
char str[100];
int alphabets, digits, others, i;
alphabets = digits = others = i = 0;
/* Input string from user */
printf("Enter any string : ");
gets(str);
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
{
others++;
}
i++;
}
printf("Alphabets = %d\n", alphabets);
printf("Digits = %d\n", digits);
printf("Special characters = %d", others);
return 0;
}
Output
Enter any string : Today is January 23
Alphabets = 14
Digits = 2 S
pecial characters = 3
0 Comments