Thursday, December 4, 2014

BlueJ Program On Displaying The Word With Maximum Vowels In A Sentence



In this program user will entyer any sentence and the program will display the word with maximum number of vowelsFor example, if the entered sentence is : This year 2014 was so so, the output will be year.

In this program we have to break the entered sentence in words and ISC students can use StringTokenizer class to break the sentence into tokens where as ICSE students can do the same job using substring () and indexOf() function of String class.

First Program is using StringTokenizer class 


import java.io.*;
import java.util.*;
class St
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str,s1,maxword;
    StringTokenizer stk;
    int i,len,max=0,c;
    public void show()throws Exception
    {
         System.out.print("\nEnter the sentence:");
         str=br.readLine();
         stk=new StringTokenizer(str);
         while(stk.hasMoreTokens())
         {
              c=0;
              s1=stk.nextToken();
              len=s1.length();
              for(i=0;i< len;i++)
              {
                 switch(s1.charAt(i))
                  {
                     case 'a':
                     case 'e':
                     case 'i':
                     case 'o':
                     case 'u':
                      case 'A':
                     case 'E':
                     case 'I':
                     case 'O':
                     case 'U':
                     c++;
                     break;
                    }
                }
                if(c >max)
              {
                  max=c;
                  maxword=s1;
                }
            }
           
         System.out.print("\nWord with maximum number of vowels:"+maxword);
        }
        public static void main(String args[])throws Exception
        {
             St ob=new St();
             ob.show();
            }
        }
       
         Sample Input Output:
        
         Enter the sentence:This is an average year

        Word with maximum number of vowels:average          
      
      

       This program is using String class functions - substring () and indexOf() 



import java.io.*;
class Str
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str,s1,maxword;
  
    int i,len,max=0,c,c1;
    public void show()throws Exception
    {
         System.out.print("\nEnter the sentence:");
         str=br.readLine().trim()+" ";
       
         while(true)
         {
              c=0;
              c1=str.indexOf(" ");
              if(c1< 0)
              break;
             
              s1=str.substring(0,c1);
              str=str.substring(c1+1);
              len=s1.length();
              for(i=0;i< len;i++)
              {
                 switch(s1.charAt(i))
                  {
                     case 'a':
                     case 'e':
                     case 'i':
                     case 'o':
                     case 'u':
                      case 'A':
                     case 'E':
                     case 'I':
                     case 'O':
                     case 'U':
                     c++;
                     break;
                    }
                }
                if(c >max)
              {
                  max=c;
                  maxword=s1;
                }
            }
           
         System.out.print("\nWord with maximum number of vowels:"+maxword);
        }
        public static void main(String args[])throws Exception
        {
             Str ob=new Str();
             ob.show();
            }
        }
       

        Sample Input Output:

      
        Enter the sentence:year 2015 may be a productive year for India

       Word with maximum number of vowels:productive
      


       Related Post:  BlueJ Programs on String/Sentence

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner