write a program to print the longest string in an array
Answers
Answered by
2
Question:-
Write a program to print the longest string in an Array.
Program:-
import java.util.*;
class Longest_String
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int n=sc.nextInt();
String s[]=new String[n], largest="";
int c=0;
System.out.println("Enter the strings in array. ");
for(int i=0;i<n;i++)
{
System.out.print("Enter: ");
s[i]=sc.nextLine();
if(s[i].length()>largest.length())
largest=s[i];
}
System.out.println("Largest String in the given array is: "+largest);
}
}
Similar questions