Write a program to input a sentence and print the penultimate word of this sentence. Penultimate
word is the second last word of a sentence. (Assume that the Sentence is of 2 or more words)
E.g. Input : The quick brown fox jumps over a lazy dog. Output : lazy
Answers
Answered by
1
Explanation:
import java.util.*;
public class Exercise60 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Input a Sentence: ");
String line = in.nextLine();
String[] words = line.split("[ ]+");
System.out.println("Penultimate word: "+words[words.length - 2]);
}
}
Answered by
0
Answer:
words = (input("Enter a sentence: ")).split(" ")
print(word[-2])
Similar questions