Return Vowels in C#

40 views
Skip to first unread message

DotnetBeginner

unread,
Dec 17, 2008, 10:56:17 PM12/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I am trying to return num of vowels present in a string using c#. I am
not getting that. Can any body have any ideas

Appreciated.

Joe Enos

unread,
Dec 18, 2008, 1:52:31 AM12/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Remember that a string is just a collection of characters. Once you
realize that, it should be pretty straightforward. I won't spoil the
fun of your homework assignment by going any further...

Of course, your definition of what a vowel is may differ - whether
it's just aeiouAEIOU, or if you're dealing with accented and other
special letters...

sitaram koundinya potula

unread,
Dec 18, 2008, 6:05:26 AM12/18/08
to DotNetDe...@googlegroups.com
Use character array and try it...........still u dont get it..then i will help in detail
-Ram
--

Cerebrus

unread,
Dec 18, 2008, 1:49:26 PM12/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I would do it the following way :

1. Create a char array comprising of the vowel letters.
2. Run a While loop searching within the string from position 0 and
looping each time you find a vowel, this time starting with the
position at which find was successful.

Happy Homework-ing !!

DotnetBeginner

unread,
Dec 24, 2008, 6:30:52 PM12/24/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Guys,

The easy way for beginners to count vowels in a string is

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the string");
string chr = Console.ReadLine().ToLower();
char ch2 = 'a';
char ch3 = 'e';
char ch4 = 'i';
char ch5 = 'o';
char ch6 = 'u';
int j = 0;

foreach (char ch in chr)
{

if (ch == ch2)

j++;
else
if (ch == ch3)

j++;
else
if (ch == ch4)
j++;
else
if (ch == ch5)
j++;
else
if (ch == ch6)
j++;
}
Console.WriteLine(j);
}
}
}


On Dec 18, 1:49 pm, Cerebrus <zorg...@sify.com> wrote:
> I would do it the following way :
>
> 1. Create a char array comprising of the vowel letters.
> 2. Run a While loop searching within the string from position 0 and
> looping each time you find a vowel, this time starting with the
> position at which find was successful.
>
> Happy Homework-ing !!
>
> On Dec 18, 8:56 am, DotnetBeginner <sushma...@gmail.com> wrote:
>
> > I am trying to return num ofvowelspresent in a string using c#. I am

Brandon Betances

unread,
Dec 24, 2008, 7:25:15 PM12/24/08
to DotNetDe...@googlegroups.com
DAMN. Endless for-else loops and an undeclared variable. sweet.

sushma sushma

unread,
Dec 24, 2008, 10:31:37 PM12/24/08
to DotNetDe...@googlegroups.com
At Least tried will learn soon proper coding, like seniors.
--
Thanks,

Sushma

Email: sush...@gmail.com

Mobile: (571) 438 1206

Lav G

unread,
Dec 24, 2008, 10:34:30 PM12/24/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

Use regular expressions

void ShowVowels(string input)
{
string str = "(a|e|i|o|u)";
Regex expression = new Regex(str);
if (expression.IsMatch(input))
{
MatchCollection value = expression.Matches(input);
foreach (Match m in value)
{
Console.WriteLine(m.Value);
}
}
}

Lav
http://lavbox.blogspot.com

sushma sushma

unread,
Dec 25, 2008, 12:54:59 AM12/25/08
to DotNetDe...@googlegroups.com
Here you go.


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the string");
            string chr = Console.ReadLine().ToLower();
            char ch2 = 'a';
            char ch3 = 'e';
            char ch4 = 'i';
            char ch5 = 'o';
            char ch6 = 'u';
            int j = 0;

            foreach (char ch in chr)
            {

                if ((ch == ch2)|(ch == ch3)|(ch == ch4)|(ch == ch5)|(ch == ch6))
                     j++;
              }
            Console.WriteLine(j);
        }
    }
}


On Wed, Dec 24, 2008 at 7:25 PM, Brandon Betances <bbet...@gmail.com> wrote:

Cerebrus

unread,
Dec 25, 2008, 4:21:29 AM12/25/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is the solution I sent on the 20th, in response to Sushma's email
to me :

---
private void FindVowels(string input)
{
char[] vowels = new Char[] { 'a', 'e', 'i', 'o', 'u' };
int pos = 0;
bool found = false;
do
{
pos = input.IndexOfAny(vowels, pos);
if(pos > -1)
{
pos += 1;
Console.WriteLine("Vowel found at position: {0}", pos.ToString
());
if(!found)
found = true;
}
} while(pos > -1);

if(found == false)
Console.WriteLine("No Vowels found in the supplied string.");
}
---

Comments/improvements are appreciated.

On Dec 25, 10:54 am, "sushma sushma" <sushma...@gmail.com> wrote:
> Here you go.
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace ConsoleApplication3
> {
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             Console.WriteLine("Enter the string");
>             string chr = Console.ReadLine().ToLower();
>             char ch2 = 'a';
>             char ch3 = 'e';
>             char ch4 = 'i';
>             char ch5 = 'o';
>             char ch6 = 'u';
>             int j = 0;
>
>             foreach (char ch in chr)
>             {
>
>                 if ((ch == ch2)|(ch == ch3)|(ch == ch4)|(ch == ch5)|(ch ==
> ch6))
>                      j++;
>               }
>             Console.WriteLine(j);
>         }
>     }
>
> }
>
> On Wed, Dec 24, 2008 at 7:25 PM, Brandon Betances <bbetan...@gmail.com>wrote:
>
>
>
>
>
>
>
> > DAMN. Endless for-else loops and an undeclared variable. sweet.
>
> > On Wed, Dec 24, 2008 at 6:30 PM, DotnetBeginner <sushma...@gmail.com>
> Email: sushma...@gmail.com
>
> Mobile: (571) 438 1206- Hide quoted text -
>
> - Show quoted text -

santhosh vs

unread,
Dec 25, 2008, 11:50:11 PM12/25/08
to DotNetDe...@googlegroups.com
he he, Homework . But he posted the answer also. So i like it. others will go by the question itself.
--
My Web Site
http://everlovingyouth.googlepages.com
My Technical Blog
http://acutedeveloper.blogspot.com
Skype :santhoshnta
Orkut :everlovingyouth
Reply all
Reply to author
Forward
0 new messages