Friday, April 1, 2016

BlueJ Program To Find The Words With Vowel From A Sentence



 In this BlueJ program, user will enter a sentence and program will display the Words with at least one vowel.

  Codes of the program is given below

import java.io.*;
public class Vowel
{
    String st1,st2;
    BufferedReader br =new BufferedReader (new InputStreamReader(System.in));
    int i;
 
    public Vowel ()
    {
      
    }

   public void takeSentence() throws Exception
   {
        System.out.println("Enter the sentence:");
        st1=br.readLine();
                System.out.println("Words containing vowels: ");
        while(true)
        {
             i=st1.indexOf(" ");
             if(i==-1)
             break;
             st2=st1.substring(0,i);
             st1=st1.substring(i+1);
            if(vowel(st2))
            System.out.print("  "+st2);
                
            }
            if(vowel(st1))
            System.out.print("  "+st1);
            }
            public boolean vowel(String s)
            {
                 int i, len,c=0;
                 s=s.toLowerCase();
                 len=s.length();
                 for(i=0;i< len; i++)
                 {
                      switch(s.charAt(i))
                      {
                           case 'a':
                           case 'e':
                           case 'i':
                           case 'o':
                           case 'u':
                           c=1;
                        }
                         if(c==1)
                         break;
                        }
                        if(c==1)
                        return true;
                        else
                        return false;
                    }
            public static void main(String args[])throws Exception
            {
                 Vowel ob=new Vowel ();
                 ob.takeSentence();
                }
            }
      

Details and Technical analysis of the program


 In this program I have used 'indexOf ()' and 'substring ()' functions of String class to break the sentence
 into Words. Function 'indexOf ()' has many overloaded versions and in this program the function taked a string value as argument and searches the
 string value in the invoking string object. The function returns the first location of the argument string if found and if
 the argument value is not present in the invoking string object it returns -1.

'substring ()' function extracts a part of the invoking string depending on the argument. The call 'st2=st1.substring(0,i);'
extracts a Word and the second call 'st1=st1.substring(i+1);' extracts the remaining string.

From the loop body, each word is passed to another user defined function 'public boolean vowel (String s)'
function and this function returns true if the argument string value contains at least one vowel or returns false.




The same program using StringTokenizer class (For ISC students)

import java.io.*;
import java.util.*;
public class Vowel
{
    String st1,st2;
    BufferedReader br =new BufferedReader (new InputStreamReader(System.in));
    StringTokenizer st;
    int i;
 
    public Vowel ()
    {
      
    }

   public void takeSentence() throws Exception
   {
        System.out.println("Enter the sentence:");
        st1=br.readLine();
        st=new StringTokenizer(st1);
                System.out.println("Words containing vowels: ");
        while(st.hasMoreTokens())
        {
            st1=st.nextToken();
           
            if(vowel(st1))
            System.out.print("  "+st1);
            }
        }
            public boolean vowel(String s)
            {
                 int i, len,c=0;
                 s=s.toLowerCase();
                 len=s.length();
                 for(i=0;i< len; i++)
                 {
                      switch(s.charAt(i))
                      {
                           case 'a':
                           case 'e':
                           case 'i':
                           case 'o':
                           case 'u':
                           c=1;
                        }
                         if(c==1)
                         break;
                        }
                        if(c==1)
                        return true;
                        else
                        return false;
                    }
            public static void main(String args[])throws Exception
            {
                 Vowel ob=new Vowel ();
                 ob.takeSentence();
                }
            }
           
         Related Post: BlueJ Programs on String/Sentence
           

Sample input and output


Enter the sentence:
This is a typical my style
Words containing vowels:
  This  is  a  typical  style


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner