Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int vowel;
            vowel=0;
            int i=0;
            Console.WriteLine("Enter Your Text:\n");
            char[] stringName = new char[10];
            for (int u = 0; u < stringName.length; u++)
            {
                stringName[u] =Console.ReadLine();
            }
            Console.WriteLine("Your text size is {0} character long\n",stringName.Length);
            while (i <stringName.Length)
            {
                if (stringName[i] == 'a' || stringName[i] == 'e' || stringName[i] == 'i' || stringName[i] == 'o' || stringName[i] == 'u' || stringName[i] == 'A' || stringName[i] == 'E' || stringName[i] == 'I' || stringName[i] == 'O' || stringName[i] == 'U')
                    vowel++;
                i++;

            }
            Console.WriteLine(vowel);
            Console.ReadLine();

        }
    }
}
Posted
Updated 28-Nov-13 6:05am
v2
Comments
AnthonyMG 28-Nov-13 12:09pm    
Check the Solution provided and let me know if this is what you expect..

You can try this code.

C#
static void Main()
{
    // Build a list of vowels up front:
    var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };

    Console.WriteLine("Enter a Sentence");
    string sentence = Console.ReadLine().ToLower();

    int total = sentence.Count(c => vowels.Contains(c));
    Console.WriteLine("Your total number of vowels is: {0}", total);
    Console.ReadLine();
}
 
Share this answer
 
Comments
AnthonyMG 28-Nov-13 12:18pm    
This solution is optimized one...check it.
BillWoodruff 28-Nov-13 17:08pm    
+5 Great answer !
AnthonyMG 29-Nov-13 3:50am    
Thanks BillWoodruff
Don't use a character array for your input, as you can create a string directly like this:
C#
Console.WriteLine("Enter Your Text:");
string stringName = Console.ReadLine();
Console.WriteLine("Your text size is {0} character long", stringName.Length);

Also you do not need to put the \n at the end of your format string, since Console.WriteLine adds it automatically.
 
Share this answer
 
try this too..


C#
//using System;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            int vowel = 0, number = 0,specialcharacters =0;



            Console.WriteLine("Enter Your Text:\n");
            string name = Console.ReadLine();
            Console.WriteLine("Your text size is {0} character long\n", name.Length);
            char[] vowels = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
            char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
            char[] specialCharacters = { '!','@',' ','$','%','^','*','&'};
            foreach (char c in name)
            {
                if (vowels.Contains(c))
                    vowel++;
                if (numbers.Contains(c))
                    number++;
                if (specialCharacters.Contains(c))
                    specialcharacters++;

            }
            Console.WriteLine("vowels count =" + vowel);
            Console.WriteLine("numbers count =" + number);
            Console.WriteLine("special characters count =" + specialcharacters);
            Console.ReadLine();

        }
    }
}
 
Share this answer
 
Comments
Thanks7872 28-Nov-13 13:57pm    
Why did you post 3 solution to one question? Dont do this. It is Abuse.
Karthik_Mahalingam 28-Nov-13 14:00pm    
ok sorry for it. i thought of making him understand, so thought of giving in 3 versions of answers. i wont repeat sorry.
Thanks7872 28-Nov-13 14:03pm    
Its ok. But in such cases, provide all the solutions in one answer.
Karthik_Mahalingam 28-Nov-13 14:05pm    
ok i am new to code project ,i am active on this forum from last 10 days only..
but i created account befre 3 yrs..
I am voting for AnthonyMG's solid answer above; I'll show a non-Linq solution here just for the sake of having a variety of answers.
C#
private char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };

private int CountVowels(string theString)
{
    return theString.Split(vowels, StringSplitOptions.RemoveEmptyEntries).Length;
}
Sample test:
int total = CountVowels("when 9999 \r\n in the course 3456of human events \a it becomes necessary");
Note in the test the use of the only vowel, 'a,' that is valid as an escaped character.
 
Share this answer
 
HI just made slight modification to your code..

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            int vowel;
            vowel = 0;
            int i = 0;
            Console.WriteLine("Enter Your Text:\n");
            char[] stringName = new char[10];
            for (int u = 0; u < stringName.Length; u++)
            {
                stringName[u] = Console.ReadKey(false).KeyChar;
            }
            Console.WriteLine("Your text size is {0} character long\n", stringName.Length);
            while (i < stringName.Length)
            {
                if (stringName[i] == 'a' || stringName[i] == 'e' || stringName[i] == 'i' || stringName[i] == 'o' || stringName[i] == 'u' || stringName[i] == 'A' || stringName[i] == 'E' || stringName[i] == 'I' || stringName[i] == 'O' || stringName[i] == 'U')
                    vowel++;
                i++;

            }
            Console.WriteLine(vowel);
            Console.ReadLine();

        }
    }
}
 
Share this answer
 
Comments
Durjoy39 28-Nov-13 13:21pm    
your solution is right but. if a press a sentence that 6 character long ur code show me 10 character because it takes 4 times enter key. please give me a solution
Karthik_Mahalingam 28-Nov-13 13:23pm    
check solution 4.. its my way of coding style.. u can use that..
Durjoy39 28-Nov-13 13:25pm    
i want to check vowel, consonant, digit and others. thats why if u don't mind can u solve my code.
Karthik_Mahalingam 28-Nov-13 13:27pm    
sure, pls post ur requirement in detail...
Durjoy39 28-Nov-13 13:30pm    
i want to find out vowel, consonant, digit, space thats type of thing. if i can solve my code then by using ascii code i can solve all other requirement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900