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

  • برمجة جافا
  • برمجة

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

الأجوبة

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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...