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
C# Exercise Example
// 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++;
}
}
}
}