In: Computer Science
write a java prog that include an array of strings of size
50
String[] strings = new String[50];
then find the max length of the inputs inside a method
keep reading from the user until user enters keyword to stop
input :
may ala
jony ram
asd fgghff
daniel
jwana
output :
max length : 10
CODE IN JAVA :
import java.util.*;
public class Main
{
public static int find_max_length(String[] strings,int size) //
method to find maximum length
{
int max_length = 0; // initialised to 0
for(int i = 0; i < size; i++)
{
if(max_length < strings[i].length()) // updating if
required
{
max_length = strings[i].length();
}
}
return max_length;
}
public static void main(String[] args) {
String[] strings = new
String[50];
int i = 0;
System.out.println("Enter string:
");
Scanner sc = new
Scanner(System.in);
strings[i] = sc.nextLine();
// Input string until you enter
"stop" using for loop
for(i = 1; i < 50 &&
!strings[i-1].equals("stop"); i++)
{
strings[i] = sc.nextLine();
}
int maxLength =
find_max_length(strings,i); // function called
System.out.println("Max length: " +
maxLength); // print the length
}
}
INPUT SNIPPET :
OUTPUT SNIPPET :
Hope this resolves your doubt.
Please give an upvote if you liked my solution. Thank you :)