Answer:
public static String lexLargest(String text){
//split the text based on whitespace into individual strings
//store in an array called words
String[] words = text.split("\\s");
//create and initialize a temporary variable
int temp = 0;
//loop through the words array and start comparing
for(int i = 0; i < words.length; i++){
//By ignoring case,
//check if a word at the given index i,
//is lexicographically greater than the one specified by
//the temp variable.
if(words[i].compareToIgnoreCase(words[temp]) > 0){
//if it is, change the value of the temp variable to the index i
temp = i;
}
}
//return the word given by the index specified in the temp
// variable
return words[temp];
}
Sample Output:
If the text is "This is my name again", output will be This
If the text is "My test", output will be test
Step-by-step explanation:
The code above has been written in Java. It contains comments explaining the code. Sample outputs have also been given.
However, it is worth to note the method that does the actual computation for the lexicographical arrangement. The method is compareToIgnoreCase().
This method, ignoring their cases, compares two strings.
It returns 0 if the first string and the second string are equal.
It returns a positive number if the first string is greater than the second string.
It returns a negative number if the first string is less than the second string.