icse-promo

Question

 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:
(i) Convert the first letter of each word to uppercase.
(ii) 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 following inputs:

				
					Example 1:
INPUT: Intelligence plus character is education.
OUTPUT:
Intelligence Plus Character Is Education.

Word            Vowels        Consonants
Intelligence    5             7
Plus            1             3
Character       3             6
Is              1             1
Education       5             4

Example 2:
INPUT: God is great.
OUTPUT:
God is great.

Word        Vowels        Consonants
God         1             2
Is          1             1
Great       2             3

Example 3:
INPUT: All the best!
OUTPUT:
Invalid input.
				
			

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class StringProgram
{
    public static void main(String args[])
    {
        String sen="",wd="",upper="";
        char ch=' ',last=' ';
        int i=0,len=0,vowelsFrequency=0,consonantsFrequency=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("String: ");
        sen = sc.nextLine();
        sen = sen.trim();
        len = sen.length();
        last = sen.charAt(len - 1);
        if(last != '.' && last != '?')
        {
            System.out.println("Invalid input.");

        }
        else
        {
            System.out.println("Word\tVowels\tConsonants");
            for(i = 0; i < len; i++)
            {
                ch = sen.charAt(i);
                if(ch == ' ' || ch == '.' || ch == '?')
                {
                    wd=Character.toUpperCase(wd.charAt(0))+wd.substring(1).toLowerCase();

                    System.out.println(wd+"\t"+vowelsFrequency+"\t"+consonantsFrequency);
                    wd = "";
                    vowelsFrequency=0;
                    consonantsFrequency=0;
                }

                else
                {
                    wd += ch;
                    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
                    {
                        vowelsFrequency++;
                    }
                    else
                    {
                        consonantsFrequency++;
                    }
                }
            }
        }
    }  
}




				
			

Coding Store

Leave a Reply

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