Function Countdv - C# Programming Exercise

In this exercise of C#, you will learn how to create a function that calculates the amount of numeric digits and vowels a text string contains. The function will accept three parameters: the text string we want to analyze, the variable that returns the number of digits, and the variable that returns the number of vowels, in that order. The function will be called "CountDV". This exercise is great for practicing how to manipulate text strings, how to count specific characters within a string, and how to use reference parameters in C#. With this exercise, you will be able to efficiently count both numeric digits and vowels in any given text, like in the example: CountDV("This is the phrase 12", ref amountOfDigits, ref amountOfVowels), where amountOfDigits would be 2 and amountOfVowels would be 5. This type of function can be useful in situations where you need to process text to extract specific information.

With this exercise, you will improve your ability to work with strings, count specific characters, and handle reference parameters in C#, which will allow you to develop more complex and efficient programs.

 Category

Functions

 Exercise

Function Countdv

 Objective

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

 Write Your C# Exercise

// Import the System namespace to use basic classes like Console
using System;

class Program
{
    // Main method to drive the program
    public static void Main()
    {
        // Declare variables to hold the count of digits and vowels
        int amountOfDigits = 0;
        int amountOfVowels = 0;

        // Call the CountDV function to count digits and vowels in a string
        CountDV("This is the phrase 12", ref amountOfDigits, ref amountOfVowels);

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

    // Function to count the number of digits and vowels in a string
    public static void CountDV(string text, ref int digits, ref int vowels)
    {
        // Loop through each character in the string
        foreach (char c in text)
        {
            // Check if the character is a digit
            if (char.IsDigit(c))
            {
                digits++;
            }
            // Check if the character is a vowel (both uppercase and lowercase)
            else if ("aeiouAEIOU".Contains(c))
            {
                vowels++;
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming 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#.

  •  Function IsAlphabetic

    In this exercise of C#, you will learn how to create a function that tells if a character is alphabetic (from A to Z) or not. This function should be us...

  •  Function IsNumber

    In this exercise of C#, you will learn how to create a function that tells if a text string represents an integer number. This function should be used l...

  •  Function calculator, params of Main

    In this exercise of C#, you will learn how to create a program that performs mathematical operations such as addition, subtraction, multiplication, or division...

  •  Function calculator, params and return value of Main

    In this exercise of C#, you will create a program that calculates mathematical operations like addition, subtraction, multiplication, or division, by analyzing...

  •  Function MinMaxArray

    In this exercise of C#, you will create a function named MinMaxArray that receives an array of numbers and returns the minimum and maximum values using ...

  •  Function Reverse, recursive

    In this exercise of C#, you need to create a program that uses recursion to reverse a string of characters. The program should receive a string as input...