c-sharp
  1. c-sharp-consonants-and-so-on

Consonants and so on - (C# Basic Programs)

In this tutorial, we'll learn how to write a C# program to count the number of vowels, consonants, digits, and spaces in a given string.

Syntax

The syntax for counting vowels, consonants, digits, and spaces in a string using C# is as follows:

string str = "Hello World";
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
for (int i = 0; i < str.Length; i++)
{
    char ch = str[i];
    if (Char.IsLetter(ch))
    {
        ch = Char.ToLower(ch);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        {
            vowels++;
        }
        else
        {
            consonants++;
        }
    }
    else if (Char.IsDigit(ch))
    {
        digits++;
    }
    else if (Char.IsWhiteSpace(ch))
    {
        spaces++;
    }
}
Console.WriteLine("Vowels: " + vowels);
Console.WriteLine("Consonants: " + consonants);
Console.WriteLine("Digits: " + digits);
Console.WriteLine("Spaces: " + spaces);

Example

Let's say we have a string "Hello World". Applying the above syntax, we get:

string str = "Hello World";
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
for (int i = 0; i < str.Length; i++)
{
    char ch = str[i];
    if (Char.IsLetter(ch))
    {
        ch = Char.ToLower(ch);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        {
            vowels++;
        }
        else
        {
            consonants++;
        }
    }
    else if (Char.IsDigit(ch))
    {
        digits++;
    }
    else if (Char.IsWhiteSpace(ch))
    {
        spaces++;
    }
}
Console.WriteLine("Vowels: " + vowels);
Console.WriteLine("Consonants: " + consonants);
Console.WriteLine("Digits: " + digits);
Console.WriteLine("Spaces: " + spaces);

Output:

Vowels: 3
Consonants: 7
Digits: 0
Spaces: 1

Thus, the output shows that the given string has 3 vowels, 7 consonants, 0 digits, and 1 space.

Explanation

The program counts the number of vowels, consonants, digits, and spaces in a given string by looping through each character in the string and checking if it's a letter, digit, or space. If the character is a letter, it checks whether it is a vowel or consonant. If the character is a digit, it increments the count of digits. If the character is a space, it increments the count of spaces.

Use

This program is useful in scenarios where we need to analyze the statistics of a given string. For instance, we can use this program to analyze the frequency of vowels, consonants, digits, and spaces in a given message, helping us understand the data better.

Summary

In this tutorial, we have learned how to write a C# program to count the number of vowels, consonants, digits, and spaces in a given string. We have discussed the syntax, example, explanation, and use of the C# program to analyze the statistics of a string. Applying this program in real-world scenarios enhances one's coding ability and problem-solving skills.

Published on: