KnowledgeBoat Logo
|

Computer Applications

Consider a String:
THE COLD WATER AND THE HOT WATER GLASSES ARE KEPT ON THE TABLE

Write a program by using scanner class to enter the string and display the new string after removing repeating token 'THE'. The new string is:

COLD WATER AND HOT WATER GLASSES ARE KEPT ON TABLE

Java

Input in Java

4 Likes

Answer

import java.util.Scanner;

public class KboatRemoveThe
{
    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;
            else if (!word.toUpperCase().equals("THE"))
                System.out.print(word + " ");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Consider a String: THE COLD WATER AND THE HOT WATER GLASSES ARE KEPT ON THE TABLE Write a program by using scanner class to enter the string and display the new string after removing repeating token 'THE'. The new string is: COLD WATER AND HOT WATER GLASSES ARE KEPT ON TABLE

Answered By

3 Likes


Related Questions