String Question 5

Home / Java Practice Question / String Question 5

String Question 5


Question 5. Write a Program to display a longest name in array.

Program-


class Saksham {

    String longest(String names[], int n) {        

        int maxValue=0;

        String longestName=names[0];        

        for(String name: names){            

            int length=name.length();            

            if(length>maxValue){              

                maxValue=length;

                longestName=name;

            }

        }

        return longestName;

    }

}