Write a C# program to count vowels in a string
Object:
Write C# a program that contains a class that has a method that takes user name as input and second functions which returns the number of vowels present in it and the Main program prints the number of vowels, C# program to count vowels in a string.C# Projects with Source Code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab_3b
{
class name
{
public void name()
{
string name;
Console.WriteLine(“Please Enter Your Name and Check how many vowels present in your Name. “);
name = Console.ReadLine();
vow(name);
}
void vow(string n)
{
int vol=0, len = n.Length;
for (int k = 0; k<len; k++)
{
if (n[k] == ‘a’ || n[k] == ‘e’ || n[k] == ‘i’ || n[k] == ‘o’ || n[k] == ‘u’ || n[k] == ‘A’ || n[k] == ‘E’ || n[k] == ‘O’ || n[k] == ‘U’)
{
vol++;
}
}
Console.WriteLine(“\n\nNumber of Vowels in this name: {0}”, vol);
}
}
class Program
{
static void Main()
{
names i = new names();
i.nam();
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lab_3b { class names { public void nam() { string name; Console.WriteLine("Please Enter Your Name and Check how many vowels present in your Name. "); name = Console.ReadLine(); vow(name); } void vow(string n) { int vol=0, len = n.Length; for (int k = 0; k<len; k++) { if (n[k] == 'a' || n[k] == 'e' || n[k] == 'i' || n[k] == 'o' || n[k] == 'u' || n[k] == 'A' || n[k] == 'E' || n[k] == 'O' || n[k] == 'U') { vol++; } } Console.WriteLine("\n\nNumber of Vowels in this name: {0}", vol); } } class Program { static void Main() { names i = new names(); i.nam(); Console.ReadKey(); } } }
Output:
Another C# Program: C# Program Calculating the Area of a Rectangle
Another C# Program: C# Program that Subtracts two user-defined numbers
Pingback: C# how to handle exceptions to illustrate try-catch action with Exceptions
Pingback: Top 20 C# programs examples in 2021