Problem Description
Write a C program to count total number of alphabets, digits or special characters in a string using loop. How to find total number of alphabets, digits and special characters in a string in C programming.CODING ARENA
#include <stdio.h>
int main()
{
char str[100];
int i, alphabets, digits, special;
i = alphabets = digits = special = 0;
scanf("%s",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
special++;
i++;
}
printf("Alphabets=%d", alphabets);
printf("\nDigits=%d", digits);
printf("\nSpecial characters=%d", special);
return 0;
}
Test Case 1
Input (stdin)SRM!University@Learning&Centre@1113
Expected OutputAlphabets=27 Digits=4 Special characters=4
Test Case 2
Input (stdin)SRM1114###!!!@@@
Expected OutputAlphabets=3 Digits=4 Special characters=9
No comments:
Post a Comment