题目描述
求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。
输入样例
one two three four five
输出样例
three
代码实现
//求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母
import java.util.Scanner;
public class chartExel {
public static void main(String[] args) {
//输入五个字符串(长度为 100以内)
Scanner scanner=new Scanner(System.in);
String input=scanner.nextLine();
//one two three four five
//正则表达式,split将字符串分割成字符串数组
//String str[]=string.split(",|\\s|!");
String str[]=input.split(" ");
String maxWord=str[0];
for (int i=0;i<str.length;i++){
//System.out.println(str[i]);
if(str[i].length() > maxWord.length()){
maxWord=str[i];
}
}
System.out.println(maxWord);
}
}
知识点
字符串方法split()根据给定正则表达式的匹配拆分此字符串。
遍历数组
本文暂时没有评论,来添加一个吧(●'◡'●)