Count Digits and Vowels in a String in C#

This C# function is designed to calculate the number of numeric digits and vowels within a given text string. The function, named "CountDV", accepts three parameters: the string to search, and two reference variables that will return the count of digits and vowels. The function will process each character in the string and update the count accordingly. After calling this function, the number of digits and vowels in the provided string will be stored in the reference variables. For example, for the input "This is the phrase 12", the count of digits would be 2, and the count of vowels would be 5.



Group

Functions in C#

Objective

1. Write a function named "CountDV" that accepts a string and two reference variables to count the digits and vowels.
2. Loop through each character in the string to check if it is a digit or a vowel.
3. Update the reference variables for digits and vowels accordingly.
4. Return the count of digits and vowels through the reference parameters.

Write a C# function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three parameters: the string that we want to search, the variable that returns the number of digits, and the number of vowels, in that order). The function should be called "CountDV". Use it like this:

CountDV ("This is the phrase 12", ref amountOfDigits, ref amountOfVowels)

In this case, amountOfDigits would be 2 and amountOfVowels would be 5

Example C# Exercise

 Copy C# Code
using System;

class Program
{
    // Function to count the digits and vowels in a string
    public static void CountDV(string text, ref int digits, ref int vowels)
    {
        // Initialize the counters for digits and vowels
        digits = 0;
        vowels = 0;

        // Convert the text to lowercase to make vowel comparison easier
        text = text.ToLower();

        // Loop through each character in the string
        foreach (char c in text)
        {
            // Check if the character is a digit
            if (Char.IsDigit(c))
            {
                digits++;  // Increment the digits counter
            }
            // Check if the character is a vowel
            else if ("aeiou".Contains(c))
            {
                vowels++;  // Increment the vowels counter
            }
        }
    }

    // Main method to test the CountDV function
    public static void Main(string[] args)
    {
        // Initialize variables to store the results
        int amountOfDigits = 0;
        int amountOfVowels = 0;

        // Call the CountDV function with a sample text
        CountDV("This is the phrase 12", ref amountOfDigits, ref amountOfVowels);

        // Output the results
        Console.WriteLine("Number of digits: " + amountOfDigits);
        Console.WriteLine("Number of vowels: " + amountOfVowels);
    }
}

 Output

Number of digits: 2
Number of vowels: 5

Share this C# Exercise

More C# Practice Exercises of Functions in C#

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  • Check if a Character is Alphabetic in C#

    This C# function is designed to check if a given character is alphabetic, meaning it falls within the range of letters A to Z (both uppercase and lowercase). The function should re...

  • Check if a String is an Integer in C#

    This C# function is designed to check if a given string represents an integer number. The function should return a boolean value indicating whether the string can be successfully p...

  • Calculator C# Program Using Command Line Parameters

    This C# program allows users to perform basic arithmetic operations such as addition, subtraction, multiplication, or division directly from the command line. The user needs to pro...

  • Arithmetic Operations with Error Codes in C#

    This C# program calculates basic arithmetic operations, such as sum, subtraction, multiplication, or division, based on the command line parameters provided by the user. The progra...

  • Min and Max Values in an Array in C#

    This C# program defines a function named "MinMaxArray" that takes an array of floating-point numbers and returns the minimum and maximum values stored in the array using reference ...

  • Reverse a String Using Recursion in C#

    This C# program demonstrates how to reverse a string using recursion. The program takes an input string and recursively processes each character to reverse the order. The recursion...