In: Computer Science
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”.
Program Style : Basic Java Programming
CODE:
import java.util.*;
class Solution
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence : ");
String sentence = sc.nextLine();
// utility class which seperates words in sentence, based on delimiter
StringTokenizer st = new StringTokenizer(sentence, " ");
String answer = "";
while(st.hasMoreElements())
{
String word = st.nextToken();
// check whether word length is greater 2
if(word.length() > 2)
answer += word.charAt(2);
}
System.out.println(answer);
}
}
refer the below image for indentation.
OUTPUT: