Q:

C program to count digits, spaces, special characters, alphabets in a string

0

C program to count digits, spaces, special characters, alphabets in a string

In this C program, we are going to learn how to count digits, spaces, special characters and alphabets?.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now