KnowledgeBoat Logo
|

Computer Science

Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

  1. Find the number of words beginning and ending with a vowel.
  2. Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1

INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Example 2

INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.

OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY

Example 3

INPUT:
LOOK BEFORE YOU LEAP.

OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP

Example 4

INPUT:
HOW ARE YOU@

OUTPUT:
INVALID INPUT

Java

Java String Handling

ICSE Prac 2016

15 Likes

Answer

import java.util.*;

public class VowelWord
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER THE SENTENCE:");
        String ipStr = in.nextLine().trim().toUpperCase();
        int len = ipStr.length();
        
        char lastChar = ipStr.charAt(len - 1);
        if (lastChar != '.' 
            && lastChar != '?' 
            && lastChar != '!') {
                System.out.println("INVALID INPUT");
                return;
        }
        
        String str = ipStr.substring(0, len - 1);
        
        StringTokenizer st = new StringTokenizer(str);
        StringBuffer sbVowel = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        int c = 0;
        
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            int wordLen = word.length();
            if (isVowel(word.charAt(0))
                && isVowel(word.charAt(wordLen - 1))) {
                    c++;
                    sbVowel.append(word);
                    sbVowel.append(" ");
            }
            else {
                sb.append(word);
                sb.append(" ");
            }
        }
        
        String newStr = sbVowel.toString() + sb.toString();
                            
        System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + c);
        System.out.println(newStr);
    }
    
    
    public static boolean isVowel(char ch) {
        ch = Character.toUpperCase(ch);
        boolean ret = false;
        if (ch == 'A'
            || ch == 'E'
            || ch == 'I'
            || ch == 'O'
            || ch == 'U')
            ret = true;
            
        return ret;
    }
}

Output

BlueJ output of Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE. Perform the following tasks: (a) Find the number of words beginning and ending with a vowel. (b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence. Test your program with the sample data and some random data: Example 1 INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3 ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL Example 2 INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2 A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY Example 3 INPUT: LOOK BEFORE YOU LEAP. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0 LOOK BEFORE YOU LEAP Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUTBlueJ output of Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE. Perform the following tasks: (a) Find the number of words beginning and ending with a vowel. (b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence. Test your program with the sample data and some random data: Example 1 INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3 ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL Example 2 INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2 A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY Example 3 INPUT: LOOK BEFORE YOU LEAP. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0 LOOK BEFORE YOU LEAP Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUTBlueJ output of Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE. Perform the following tasks: (a) Find the number of words beginning and ending with a vowel. (b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence. Test your program with the sample data and some random data: Example 1 INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3 ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL Example 2 INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2 A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY Example 3 INPUT: LOOK BEFORE YOU LEAP. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0 LOOK BEFORE YOU LEAP Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUTBlueJ output of Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE. Perform the following tasks: (a) Find the number of words beginning and ending with a vowel. (b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence. Test your program with the sample data and some random data: Example 1 INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3 ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL Example 2 INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2 A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY Example 3 INPUT: LOOK BEFORE YOU LEAP. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0 LOOK BEFORE YOU LEAP Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUT

Answered By

6 Likes


Related Questions