Problem - Vowels Frequencies
Problem - Vowels Frequencies
. Five of these (a, e, i, o and u) are classified as vowels, the remaining 21 as consonants. Almost every English word contains at least one vowel (rhythm is one of the few exceptions). In this problem you will be given a number of pieces of English text. Your task is to determine the frequency of each vowel that is found in the piece, and to display the answers sorted by frequency, highest frequency first. Where two vowels are equally frequent, they are to be displayed in alphabetical order. As you can see from the examples below, upper case and lower case letters are considered to be the same letter in this problem. Use lower case in your output. As you can see from the second example, a frequency of zero must still be displayed. INPUT FORMAT Each piece of text to be analysed is on a separate line of the input file. Each line has at most 200 characters. A single # on a line indicates the end of input. OUTPUT FORMAT Output for a problem must be on a single line. Each vowel must be output in lower case, followed by a colon, followed by the frequency of that vowel. There must be one space before the next letter, and a dot at the end. SAMPLE INPUT: This piece of text was written in the city of Auckland. ACM Programming Contest. SAMPLE OUTPUT: e:5 i:5 a:3 o:2 u:1. a:2 o:2 e:1 i:1 u:0. import java.io.*; import javax.swing.*; public class Prob{ public static void main(String args[]){
FileReader fr=null; FileWriter fw=null; BufferedReader br=null; PrintWriter pw=null; try{ fr=new FileReader("inA.txt"); fw=new FileWriter("outA.txt"); br=new BufferedReader(fr); pw=new PrintWriter(fw); String s=br.readLine(); while(s!=null){ s=process(s); //Function Call to execute required task on Line readed pw.println(s); //To save result from process function on o/p file s=br.readLine(); } pw.flush(); br.close(); pw.close(); JOptionPane.showMessageDialog(null,"Your relief is successful");// Indication of suceesfull execution } catch(Exception e){ // To catch file not found etc exception System.out.println("Error occurs in File Reading Writing"); } // To print the result on o/p file // To read Line one by one
System.exit(0); } public static String process(String s){ char t[]={'a','e','i','o','u'}; int h[]=new int[5]; for(int i=0;i<s.length();i++){ // Loop to traverse readed pattern for vovels char c=s.charAt(i); if(c=='a'||c=='A') h[0]++; else if(c=='e'||c=='E') h[1]++; else if(c=='i'||c=='I') h[2]++; else if(c=='o'||c=='O') h[3]++; else if(c=='u'||c=='U') h[4]++; } return sort(t,h); } public static String sort(char t[],int h[]){ for(int i=0;i<h.length-1;i++){ for(int j=0;j<h.length-1;j++){ // Function Call for soting // if u found then h array four index incremented // if o found then h array three index incremented // if i found then h array two index incremented // if e found then h array one index incremented // if a found then h array zero index incremented // S has input readed file line
if(h[j]<h[j+1]){ char u=t[j]; int sw=h[j]; t[j]=t[j+1]; h[j]=h[j+1]; t[j+1]=u; h[j+1]=sw; } } } String res="";
// Comparing frequency in int array h // Now swapping character and frequency // in the following lines
for(int k=0;k<h.length;k++){ // Prearing the desired output if(k<h.length-1) res=res+t[k]+":"+h[k]+" "; else res=res+t[k]+":"+h[k]+"."; } JOptionPane.showMessageDialog(null,res); return res; } }