Answer:
import java.util.Scanner;
public class LongestString
{
public static void main(String[] args) {
int count = 0, longest = 0;
Scanner input = new Scanner(System.in);
Scanner in = new Scanner(System.in);
System.out.print("Enter the size: ");
int size = in.nextInt();
String words[] = new String[size];
for(int i=0; i<size; i++) {
System.out.print("Enter a word: ");
words[i] = input.nextLine();
}
String longestWord = words[0];
for(int i=0; i<size; i++) {
if (words[i].length() > longestWord.length()) {
longestWord = words[i];
longest = words[i].length();
count = 1;
}
else if (words[i].length() == longestWord.length()) {
longest = words[i].length();
count++;
}
}
System.out.println("The longest word: " + longestWord + "\\The length: " + longest + "\\The count: "+ count);
}
}
Step-by-step explanation:
I provided the full code, getting the inputs and print the results, in addition to your request, so that it may be more clear.
-Ask the user for the array size of words, holds the strings entered by the user
- Inside the first for loop, initialize the words array
- Inside the second for loop, find the longest string, its length and the count of the string.
- Print the longest string, its length and count