Q:

Write a Java program to find the penultimate (next to last) word of a sentence

0

Write a Java program to find the penultimate (next to last) word of a sentence


Sample Output:

Input a String: The quick brown fox jumps over the lazy dog.
Penultimate word: lazy

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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]);
	 }			
}

Sample Output:

Input a Sentence: The quick brown fox jumps over the lazy dog.         
Penultimate word: lazy

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now