Saturday, January 19, 2008

Frequency Of Vowels In the String

Q Write a Program to Find the Number of times vowels Occure in a given String

Method 1 - Of anshul
import java.io.*;
class vowels{ public static void main()throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String str;
int i,c=0,j;
System.out.println("Enter a string");
str=in.readLine();
str=str.toLowerCase();
for(i=0;i<=str.length()-1;i++)
{
j=str.charAt(i);
if(j==' ')
{
if((j+1)=='a'(j+1)=='e'(j+1)=='i'(j+1)=='o'(j+1)=='u')
c++;
}
}
if(str.charAt(0)=='a'str.charAt(0)=='e'str.charAt(0)=='i'str.charAt(0)=='o'str.charAt(0)=='u')
c++;
System.out.println("\n no. of words starting with vowels::::"+c);
}
}

Method 2 (By Harsh)(me :))
import java.io.*;
class Proverbial
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter a String ");
String s=br.readLine();
char ch;
s=s.toLowerCase();
int c=0,l=s.length();
for(int i=0;i
{
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
c++;
}
System.out.println(" Frequency of Vowels is "+ c);
}
}





No comments: