Examples C

WAP in C to count all the vowels, consonants, digits..entered by user

The Program in C to count all the vowels, consonants, digits, spaces, special symbols from a given text typed by the user is given below:

#include <stdio.h>

int main()
{
    char text[1000];
    int vowels = 0, consonants = 0, digits = 0, spaces = 0, special = 0;

    printf("Enter some text: ");
    scanf("%[^\n]s",text);

    for (int i = 0; text[i] != '\0'; i++)
    {
        if (text[i] >= 'A' && text[i] <= 'Z' || text[i] >= 'a' && text[i] <= 'z') {
            if(text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u' || 
            text[i] == 'A' || text[i] == 'E' || text[i] == 'I' || text[i] == 'O' || text[i] == 'U') {
                vowels++;
            }
            else {
                consonants++;
            }
        }
        else if (text[i] >= '0' && text[i] <= '9') {
            digits++;
        }
        else if (text[i] == ' ') {
            spaces++;
        }
        else {
            special++;
        }
    }

    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    printf("Digits: %d\n", digits);
    printf("Spaces: %d\n", spaces);
    printf("Special symbols: %d\n", special);

    return 0;
}

Output:

Enter some text: 34r&A?
Vowels: 1
Consonants: 1
Digits: 2
Spaces: 0
Special symbols: 2

Pro-Tips💡

This code is a simplified version of the previous code, where I have removed the repetitive checks for vowels and added a check for alphabets before checking for vowels, also scanf is used to take input instead of gets() which is a deprecated function.


Also, I have combined the if conditions to check for vowels and consonants into one,

and removed the unnecessary else statements.

This reduces the number of checks the program needs to make, making it more efficient.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X