Computer Applications

Using scanner class, write a program to input a string and display all the tokens of the string which begin with a capital letter and end with a small letter.

Sample Input: The capital of India is New Delhi
Sample Output: The India New Delhi

Java

Input in Java

19 Likes

Answer

import java.util.Scanner;

public class KboatDisplayTokens
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence ending with a space & terminating with (.):");
        while(in.hasNext()) {
            String word = in.next();
            if (word.equals("."))
                break;
            if (Character.isUpperCase(word.charAt(0)) &&
                Character.isLowerCase(word.charAt(word.length() - 1)))
                System.out.print(word + " ");
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

6 Likes


Related Questions