In: Computer Science
In visual C#
enter a string: "Mississippi".
Results come out as" there are 11 letters with 4 vowels and 7 consonants".
Create the following methods to complete this application:
Methods
int countLetters(string userInput)
int countVowels(string userInput)
int countConsonants(string userInput)
Events
btnAnalyze_Click
HINTS:
Tutorial 8-1 p. 477-480 shows an example application that uses methods to determine whether a password has the appropriate characters based on the given criteria.
Use a "break;" clause to skip a section of an decision statement.
take 4 controls on the form & set following properties
1. Label, name = lblResults, text= Results:
2. Label, name = label2, text= Enter a string:
3. TextBox, name = txtInput
4. Button, name = btnAnalyze, text= Analyze
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace project1
{
public partial class frmCountVowels : Form
{
public frmCountVowels()
{
InitializeComponent();
}
private void btnAnalyze_Click(object sender, EventArgs e)
{
int l = 0, v = 0, c = 0;
l = countLetters(txtInput.Text); //get Letter count
v = countVowels(txtInput.Text); //get Vowel count
c = countConsonants(txtInput.Text); //get Consonants count
lblResults.Text = "There are " + l + " letters with " + v + " vowels and " + c + " consonants";
}
int countLetters(string userInput)
{
int lettersCount = 0;
foreach (char ch in userInput) //get each character from the userInput string
{
if (Char.IsLetter(ch)) //if character is a letter
{
lettersCount++; //increase count
}
}
return lettersCount;
}
int countVowels(string userInput)
{
int a = 0, e = 0, i = 0, o = 0, u = 0;
int vCount = 0;
char ch;
foreach (char letter in userInput) //get each character from the userInput string
{
ch = letter;
if (!Char.IsLetter(ch)) //if character is not letter
{
continue; //skip to next character
}
else if (ch == Char.ToUpper(ch)) //if character is uppercase
{
ch = Char.ToLower(ch); //convert it to lowercase
}
switch (ch)
{
case 'a':
a++;
vCount++;
break;
case 'e':
e++;
vCount++;
break;
case 'i':
i++;
vCount++;
break;
case 'o':
o++;
vCount++;
break;
case 'u':
u++;
vCount++;
break;
}
}
return vCount;
}
int countConsonants(string userInput)
{
int consonantsCount = 0, vowels = 0, letters = 0;
vowels = countVowels(userInput); //get Vowel count
letters = countLetters(userInput); //get Letter count
consonantsCount = letters - vowels;
return consonantsCount;
}
}
}
output