Write a source-code that counts the number of vowel, consonant letters and the total number of letters on
a text. Use this text: “This is the sample text!”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<char> vovels = new List<char>() { 'a', 'e', 'i', 'o', 'u', 'y' };
List<char> consonants = new List<char>() { 'b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 's', 't', 'v', 'x', 'z', 'h', 'r', 'w' };
string text = "This is the sample text!";
int vovelCount = 0;
int consonantCount = 0;
int totalLetters = 0;
for (int i = 0; i < text.Length; i++)
{
if (char.IsLetter(text[i]))
{
totalLetters++;
}
if (vovels.IndexOf( text.ToLower()[i]) > -1)
{
vovelCount++;
}
if (consonants.IndexOf(text.ToLower()[i]) > -1)
{
consonantCount++;
}
}
Console.WriteLine("Vovel count: {0}", vovelCount);
Console.WriteLine("Consonant count: {0}", consonantCount);
Console.WriteLine("Total letters: {0}", totalLetters);
Console.ReadKey();
}
}
}
Comments
Leave a comment