Answer:
int count = 0; //int variables
int longest =0; // int variables
Scanner input = new Scanner(System.in); // given input is reference variable
String str= new String(); // creating object of sting class
while (input.hasNext()) // taking the input by using scanner reference
{
str= input.next(); // taking input in string
if (str.length() == longest) // checking condition
++count; // increment the count
else
if (Str.length() > longest) // if string is greater then longest
{
longest = str.length(); // calculating length
count = 1; // assign count to 1
}
}
Explanation:
Following are the description of the code :
- Declared a two variable count and longest of int type.
- Create a object of String class i.e 'str".
- Taking input in the string by using scanner reference i.e "input".
- Finally checking the condition of if and else block and calculating the length of string and assign into longest variable.