How to know how many vowels are present in a word in c# [Resolved]

Posted by Shweta_Pinky under C# on 8/31/2013 | Points: 10 | Views : 1958 | Status : [Member] | Replies : 4
How to know how many vowels are present in a word in c#




Responses

Posted by: Satyapriyanayak on: 8/31/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
using System;


class Program
{
static void Main()
{
Console.Write("Please Enter a word ");
string word = Console.ReadLine();
Console.WriteLine("There are {0} vowels in {1}", CountVowels(word), word);
Console.ReadKey();
}

static int CountVowels(string word)
{
word = word.ToLower();
int count = 0;
foreach(char c in word)
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') count++;
}
return count;
}
}


If this post helps you mark it as answer
Thanks

Shweta_Pinky, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 8/31/2013 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi
try this code count with separate


protected void btClick_Click(object sender, EventArgs e)
{
string Txtw1 = "";
Txtw1= Txtw.Text.ToUpper();
int count = 0;
foreach (char c in Txtw1)
{

if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
count = count + 1;
if (c == 'A') { countA = countA + 1; StrAcnt = c + " " + countA.ToString(); }
if (c == 'E') { countE = countE + 1; StrEcnt = c + " " + countE.ToString(); }
if (c == 'I') { countI = countI + 1; StrIcnt = c + " " + countI.ToString(); }
if (c == 'O') { countO = countO + 1; StrOcnt = c + " " + countO.ToString(); }
if (c == 'U') { countU = countU + 1; StrUcnt = c + " " + countU.ToString(); }
}
}
Response.Write(StrAcnt + " " + StrEcnt + " " + StrIcnt + " " + StrOcnt + " " + StrUcnt);
}




Mark as Answer if its helpful to you

Shweta_Pinky, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer this link for alternate method
http://stackoverflow.com/questions/11873980/c-sharp-finding-vowels-in-a-string-using-struct-or-enum

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shweta_Pinky, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
check once the following approach to findout num of vowels in a word....

string strWord = "chandrika"; // actual string

// first get the lower case word and then replace each vowel wiyh empty space
string resWord = ((((strWord.ToLower().Replace("a", "")).Replace("e","")).Replace("i","")).Replace("o","")).Replace("u","");

// find the length of result string and actual string then calculate difference
int NumOfVowels = strWord.Length - resWord.Length;


note: i haven't tested above posted code.. if there is any syntactical error correct it and excuse me.


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shweta_Pinky, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response