Using the Java language, have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.
Input:"fun&!! time"
Output:"time"
Input:"I love dogs"
Output:"love"
package demo5;
import java.util.Scanner; class Test1 { public static String LongestWord(String sen) { int word=1; sen.trim(); for(int i=0;i<sen.length();i++){ if(sen.charAt(i)==' '){ word++; } } String[] WordNum = new String[word]; for(int i=0;i<word;i++){ WordNum[i]=""; } int[] num= new int[word]; char str; word=0; for(int i=0;i<sen.length();i++){ if((sen.charAt(i)>='a' && sen.charAt(i)<='z') || (sen.charAt(i)>='A' && sen.charAt(i)<='Z')){ str=sen.charAt(i); WordNum[word]+=str; }else if(sen.charAt(i)==' '){ word++; } } word++; int max=WordNum[0].length(); int maxindex=0; for(int i=0;i<word;i++){ num[i]=WordNum[i].length(); if(num[i]>max){ max=num[i]; maxindex=i; } } return WordNum[maxindex]; } public static void main (String[] args) { // keep this function call here Scanner s = new Scanner(System.in); System.out.print(LongestWord(s.nextLine())); } }