7 Replies - 10135 Views - Last Post: 18 February 2009 - 10:58 AM Rate Topic: -----

#1 beginner123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-February 09

counting vowels from a sentence

Posted 18 February 2009 - 06:14 AM

hi, i have been learning to program for a couple of months now, i have found it very interesting. i have tried to produce a code that can count vowels from a sentence and then output it on screen.(i have only been using CMD if this helps?).
i think i have done it right, but i cannot run it, i get a few errors which i think is stopping it from building. this is what i have come up with:

{
			string line;
			line = Console.ReadLine();			 



			char letter;
			int len = line.Length;			   
			for (int n = 0; n < len; n++)
			{
				letter = line[n];			   

				switch (currentLetter)
				{
					case 'A': vowel++;
							 break;

					case 'E':vowel++;
							 break;

					case 'I':vowel++;
							 break;

					case 'O':vowel++;
							 break;

					case 'U':vowel++;
							 break;
						vowels++;
						break;
				}

so this is my first attempt. could anyone give me and tips in why this will not run, and if i have gone wrong could you explain and show me where? would be a great help and i would appreciate it :^:

thanks a lot
:)

Is This A Good Question/Topic? 0
  • +

Replies To: counting vowels from a sentence

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: counting vowels from a sentence

Posted 18 February 2009 - 07:16 AM

1. it would be nice if you posted the build errors. believe it or not, they tell you exactly what the problem is and where it is(line number).

2. inside your for loop, you are setting the letter from the sentence to "letter", but you are running your switch statement on "currentLetter".

3. remove the last "vowels++" and "break;" that are not inside of a case statement.

4. you are only checking uppercase vowels.

This post has been edited by eclipsed4utoo: 18 February 2009 - 07:22 AM

Was This Post Helpful? 1
  • +
  • -

#3 ItaSka  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 06-February 09

Re: counting vowels from a sentence

Posted 18 February 2009 - 07:23 AM

I think this maybe work...

   string line;
			line = Console.ReadLine().ToUpper();
			int vowel = 0;
			for (int n = 0; n < line.Length; n++)
			{
				switch (line[n])
				{
					case 'A': vowel++;
						break;

					case 'E': vowel++;
						break;

					case 'I': vowel++;
						break;

					case 'O': vowel++;
						break;

					case 'U': vowel++;
						break;
				 }
			}
			Console.WriteLine("The number of vowels in the word "+line+" is "+vowel);



I take your code and eliminate mistakes.

Bye ;)
Was This Post Helpful? 1
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: counting vowels from a sentence

Posted 18 February 2009 - 07:24 AM

here is how I would have done it.

char[] charArray = new char[] {'a','e','i','o','u' };
string line = Console.ReadLine().ToLower();

char letter;
int vowels = 0;

for (int i = 0; i < line.Length; i++)
{
	letter = line[i];

	if (charArray.Contains(letter))
		vowels++;
}

Console.WriteLine("Number of vowels in sentence is {0}", vowels);
Console.Read();


Was This Post Helpful? 1
  • +
  • -

#5 beginner123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-February 09

Re: counting vowels from a sentence

Posted 18 February 2009 - 08:38 AM

hi thanks for the tips, this is what i have now got.
also yeh i needed to check lower case aswell, would be helpful :P

string line;
			line = Console.ReadLine().ToUpper();
			int vowel = 0;
			for (int n = 0; n < line.Length; n++)
			{
				switch (line[n])
				{
					case 'A': vowel++;
						break;

					case 'a': vowel++;
						break;

					case 'E': vowel++;
						break;

					case 'e': vowel++;
						break;

					case 'I': vowel++;
						break;

					case 'i': vowel++;
						break;

					case 'O': vowel++;
						break;

					case 'o': vowel++;
						break;

					case 'U': vowel++;
						break;

					case 'u': vowel++;
						break;
				}
			}
			Console.WriteLine("The number of vowels are: " + line + " is " + vowel);
			Console.ReadLine(); 


i have built this and i had a couple of errors, but i just had a play with it and now it works and counts correctly.
i would like to say thank you for the help i have saved your messages in case i get stuck on sumthing like this again. :^:
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: counting vowels from a sentence

Posted 18 February 2009 - 08:49 AM

You don't need to check for lowercase vowels. You are setting all of the letters to uppercase when you do this statement...

line = Console.ReadLine().ToUpper();



so the parts of your switch statement that check for lowercase letters are a waste of space and code.

This post has been edited by eclipsed4utoo: 18 February 2009 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 beginner123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-February 09

Re: counting vowels from a sentence

Posted 18 February 2009 - 09:00 AM

ahh ok, i understand, thanks for the note :^:
Was This Post Helpful? 0
  • +
  • -

#8 apkamble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-January 08

Re: counting vowels from a sentence

Posted 18 February 2009 - 10:58 AM

View Postbeginner123, on 18 Feb, 2009 - 05:14 AM, said:

hi, i have been learning to program for a couple of months now, i have found it very interesting. i have tried to produce a code that can count vowels from a sentence and then output it on screen.(i have only been using CMD if this helps?).
i think i have done it right, but i cannot run it, i get a few errors which i think is stopping it from building. this is what i have come up with:

{
			string line;
			line = Console.ReadLine();			 



			char letter;
			int len = line.Length;			   
			for (int n = 0; n < len; n++)
			{
				letter = line[n];			   

				switch (currentLetter)
				{
					case 'A': vowel++;
							 break;

					case 'E':vowel++;
							 break;

					case 'I':vowel++;
							 break;

					case 'O':vowel++;
							 break;

					case 'U':vowel++;
							 break;
						vowels++;
						break;
				}

so this is my first attempt. could anyone give me and tips in why this will not run, and if i have gone wrong could you explain and show me where? would be a great help and i would appreciate it :^:

thanks a lot
:)

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1