C Program to count number of alphabets, digits and special characters using function - IProgramX

Q. Write a function that accepts a character as parameter and returns 1 if it is an alphabet, 2 if it is a digit and 3 if it is a special symbol. In main, accept characters till the user enters EOF and use the function to count the total number of alphabets, digits and special symbols entered



Program

#include <stdio.h>
void cal();
int main()
{
cal();
}
void cal()
{
    char    str[100];
    int countDigits,countAlphabet,countSpecialChar,countSpaces;
    int counter;

    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: I Program X

Digits: 0
Alphabets: 9
Spaces: 2
Special Characters: 0

Post a Comment

1 Comments

  1. Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people.. read

    ReplyDelete