icse-promo

Question

Design a class VowelWord to accept a sentence and calculate the frequency of words that begin with a vowel . The words in the input string are separated by a single blank space and is terminated by a full stop. The description of the class is given below:

Classname:VowelWord

Data members / instance variables:

str : to store a sentence

freq : store the frequency of the words beginning with a vowel

Member functions:
VowelWord( ): default constructor to initialize data member to legal initial value

void readstr(): to accept a sentence

void freq_vowel(): counts the frequency of the words that begin with a vowel

void display(): to display the original string and the frequency of the word that begin with a vowel

Specify the class VowelWord giving details of the constructor( ), void readstr( ) , void freq_vowel() and void display() . Also define the main() function to create an object and call the methods accordingly to enable the task.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class VowelWord
{
    String str;
    int freq;
    public VowelWord()
    {
        str="";
        freq=0;
    }
    public void readstr()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter a string");
        str=sc.nextLine();
    }
    public void freq_vowel()
    {
        char ch,ch1=' ';
        for(int i=0;i< str.length();i++)
        {  
            ch=str.charAt(i);
            if(i==0)
            {
                if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
                {
                    freq++;
                }
            }
            else if(ch==' ')
            {
                ch1=str.charAt(i+1);
                if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u'||ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
                {
                    freq++;
                }    
            }
        }
    }
    
    void display()
    {
        System.out.println("ORIGINALSENTENCE:  "+str);
        System.out.println("FREQUECY OF WORD BEGINNING WITH A VOWEL "+freq);
    }
    public static void main()
    {
        VowelWord ob1=new VowelWord();
        ob1.readstr();
        ob1.freq_vowel();
        ob1.display();
    }
}

       
        

       
        

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *