SoFunction
Updated on 2025-03-08

Find the longest string code instance based on java

Topic: Write a program to output the longest string for the input N strings

Enter a sample:

4
zhaoming
zhangming
sunming
liming

Output sample:

The longest string: zhangming

The code is as follows

import ;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner();
    int t = ();
    int count = 0;
    String[] arr = new String[t];
    for (int i = 0; i < t; i++) {
      arr[i] = ();
    }
    for (int i = 0; i < ; i++) {
      if (arr[i].length() > arr[count].length()){
        count = i;
      }
    }
    ("Longest string:"+arr[count]);
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.