fbpx

Write a program to accept a sentence which may be terminated by either ’.’ or  ‘?’ only. The words are to be separated by a single blank space. Print an error message if the input does not terminate with ‘.’ or ‘?’.

You can assume that no word in the sentence exceeds 15 characters, so that you get a proper formatted output.

Perform the following tasks:

  • Convert the first letter of each word to uppercase.
  • Find the number of vowels and consonants in each word and display them with proper headings along with the words.

Test your program with the below code.


import java.util.*;

class vowel

{

    public static void main(String args[])

    {

        String s,st,str=””,p;

        char c,ch;

        int i,j,m,n,t=0,vol=0,con=0;

        Scanner in=new Scanner(System.in);

        System.out.println(“Enter a sentence termenated by ‘.’ or ‘?'”);

        s=in.nextLine();

        m=s.length();

        if(s.charAt(m-1)==’.’||s.charAt(m-1)==’?’)

        {

            for(i=0;i<m;i++)

            {

                c=s.charAt(i);

                if(c==’ ‘||c==’.’||c==’?’)

                {

                    p=s.substring(t,i);

                    st=Character.toUpperCase(p.charAt(0))+p.substring(1);

                    str=str+’ ‘+st;

                    t=i+1;

                }

            }

            System.out.println(str);

           

            p=””;

            System.out.println(“Words\t”+”Vowels\t”+”Consonants”);

            str=str.trim();

            m=str.length();

            for(i=0;i<m;i++)

            {   

                if(str.charAt(i)==’ ‘)

                {

                    System.out.println(p+”\t”+vol+”\t”+con);

                    vol=0;

                    con=0;

                    p=””;

                }

                else

                {

                        ch=Character.toLowerCase(str.charAt(i));

                        if(ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’)

                        {

                            vol++;

                        }

                        else

                        {

                            con++;

                        }

                    p=p+str.charAt(i);

                }

              

            }

            System.out.println(p+”\t”+vol+”\t”+con);

          

        }

        else

        {

            System.out.println(“Invalid input”);

        }

    }

}

 

Output when you enter invalid sentence:

Enter a sentence termenated by ‘.’ or ‘?’

This is a icse program

Invalid input

Output when input in correct form

This Is A Icse Programming

Words Vowels  Consonants

This     1          3

Is         1          1

A         1          0

Icse      2          2

Programming   3          8

Leave a Reply

×