Answer on Question #48880 – Engineering – Other
Task
How to write a program to check whether the input alphabet is a vowel or not using switch case.
Solution (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threadings Tasks;
namespace IsVowel
{
class Program
{
//create the structure where we will contain statistic information about vowels
public struct Vowel
{
private int[] Position; // positions of vowel in the string
private int count; // number of vowel entries into string
private char symbol; // vowel
public Vowel(char s) // initial constructor
{
symbol = s;
count = 0;
Position = new int[256];
}
public void Add(int p) // to add the information about vowels entry
{
count++;
Position[count - 1] = p;
}
public int[] Positions // return array with positions
{
get { return Position; }
}
public char Symbol // return vowel symbol
{
get { return symbol; }
}
public int Count // return number of vowels entries
{
get { return count; }
}
}}
static void Main(string[] args)
{
// vowels structures for gathering statistic
Vowel vowelA, vowelE, vowelI, vowelO, vowelU, vowelY;
vowelA = new Vowel('a');
vowelE = new Vowel('e');
vowelI = new Vowel('i');
vowelO = new Vowel('o');
vowelU = new Vowel('u');
vowelY = new Vowel('y');
Console.WriteLine("Enter char string:");
// enter string
string str = Console.ReadLine();
// convert string symbols to low case
str = str.ToLower();
// check string chars
for(int i=0; i<str.Length; i++)
{
switch (str[i])
{
case 'a': vowelA.Add(i);
break;
case 'e': vowelE.Add(i);
break;
case 'i': vowelI.Add(i);
break;
case 'o': vowelO.Add(i);
break;
case 'u': vowelU.Add(i);
break;
case 'y': vowelY.Add(i);
break;
}
}
// printing results
Console.WriteLine("The statistic of vowels in the string is:");
Console.WriteLine("a - {0}\t e - {1}\t i - {2}\t o - {3}\t u - {4}\t, y - {5}\n",
vowelA.Count, vowelE.Count, vowelI.Count, vowelO.Count, vowelU.Count,
vowelY.Count);
Console.ReadKey();
}
}
}
http://www.AssignmentExpert.com/