Monday, June 10, 2024

Count Vowels in a String Using a Class in C#

 // VowelCounter.cs

// Author  : Dr. Jake Rodriguez Pomperada, MAED-IT, MIT, PHD-TM

// Date    : June 9, 2024  Sunday  10:49 PM

// Tools   : Microsoft Visual Studio 2022  Community Edition (64 Bit)

// Website : http://www.jakerpomperada.com

// YouTube Channel : https://www.youtube.com/jakepomperada

// Email   : jakerpomperada@gmail.com



using System;

using System.Linq;


// Define the VowelCounter class in C#

public class VowelCounter

{

    public string inputString;

    public int vowelCount;


    public VowelCounter()

    {

        inputString = "";

        vowelCount = 0;

    }


    public void GetInputString()

    {

        Console.WriteLine("\n\tCount Vowels in a String Using a Class in C#\n");

        Console.Write("\n\tEnter a string: ");

        inputString = Console.ReadLine().Trim();

    }


    public void ConvertToLowerCase()

    {

        inputString = inputString.ToLower();

    }


    public void CountVowels()

    {

        foreach (char c in inputString)

        {

            if ("aeiou".Contains(c))

            {

                vowelCount++;

            }

        }

    }


    public void DisplayResult()

    {

        Console.WriteLine("\n\tString in lowercase : " + inputString);

        Console.WriteLine("\n\n\tNumber of vowels    : " + vowelCount);

        Console.WriteLine("\n\n\tEnd of Program\n");

        Console.ReadKey();

    }

}


// Main program in C#

class Program

{

    static void Main(string[] args)

    {

        VowelCounter counter = new VowelCounter();

        counter.GetInputString();

        counter.ConvertToLowerCase();

        counter.CountVowels();

        counter.DisplayResult();

    }

}


No comments:

Post a Comment