Program to find how many letters start with vowels
The program's output is n't correct
Here's the code......
Code Java:
import java.io.*;
class Vowels
{
void Input()throws IOException
{
BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Sentence");
String b = a.readLine();
int c = b.length();
int d=0,f=0;
char e ;
for(;d<c;d++)
{
e = b.charAt(d);
if((e=='A')||(e=='E')||(e=='I')||(e=='O')||(e=='U')) \\Used for letters at beginning of Sentence
++f;
if(((e=='a')||(e=='e')||(e=='i')||(e=='o')||(e=='u'))&&((d-1)==' ')&(d!=0))
++f;
}
System.out.print(f);
}
}
I will be happy if those lengthy if conditions is shortened.
Re: Program to find how many letters start with vowels
Quote:
The program's output is n't correct
For a given input, what do you receive and what do you expect? Please provide as much detail as possible
Re: Program to find how many letters start with vowels
You code don't work because you forget of to write b.charAt(d-1). You wrote only (d-1).
Code :
if(((e=='a')||(e=='e')||(e=='i')||(e=='o')||(e=='u'))&&(b.charAt(d-1)==' ')&(d!=0))
You can reduce you code this way:
Code :
void Input()throws IOException
{
BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Sentence");
String[] b = a.readLine().toLowerCase().split("[ ]+");
int f = 0;
for(String s: b)
{
if(s.matches("[aeiou].*"))
f++;
}
System.out.println(f);
}
In line
Code :
String[] b = a.readLine().toLowerCase().split("[ ]+");
I divide the sentence in words and I passed all to lower case. The separator is one or more spaces.
In line
Code :
if(s.matches("[aeiou].*"))
I use regular expression. If word start with vowels f is increment. The .* mean any caracter.
I was helped or confused?
Re: Program to find how many letters start with vowels
What about the startsWith(String prefix) method?
String (Java Platform SE 6)
Are you asking if each letter is a vowel or if each word begins with a vowel? :confused:
I'm not familiar with the matches and the Pattern class.
What exactly is that doing?
I'm thinking it's saying that if it has any of the chars in "aeiou" to increment f.
If so, that beats my idea.
Now at least I know what that method in the String class really does. :cool::cool::cool::cool::cool:
Re: Program to find how many letters start with vowels
>What about the startsWith(String prefix) method?
You can to use startsWith. But you have to use for each letter. For example:
Code :
if(s.startsWith("a") || s.startsWith("e") || s.startsWith("i") || s.startsWith("o") || s.startsWith("u"))
f++;
>Are you asking if each letter is a vowel or if each word begins with a vowel?
On my code I see if each word begins with a vowel.
>What exactly is that doing?
In a regular expression we have some rules, for examples:
[ ] means that the character is a space or \n or \r
[ ]+ means one or more (spaces or \n or \r)
[aeiou] means that the character is a or e or i or o or u
. means one any character
.* means zero or one or more any character
So
I use the regular expresssion ("[ ]+") for divide sentence in words. And I Use ("[aeiou].*") for match any word that begins with a or e or i or o or u follow of any character. How I divide this sentence in words each word don't have spaces.
understand?