Lập trình Java - Tìm từ dài nhất trong xâu
Viết chương trình thực hiện nhập một xâu ký tự và tìm từ dài nhất trong xâu đó. Từ đó xuất hiện ở vị trí nào? (Chú ý. nếu có nhiều từ có độ dài giống nhau thì chọn từ đầu tiên tìm thấy).
Mã nguồn chương trình:
import java.util.Scanner;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ADMIN
*/
public class Bai32 {
/**
* @param args the command line arguments
*/
public static void timXauMax(String strInput){
StringTokenizer strToken= new StringTokenizer(strInput," ,\t,\r");
int Max,i=1,lengthStr;
Max= strToken.nextToken().length();
int viTriMax= i;
while(strToken.hasMoreTokens()){
lengthStr= strToken.nextToken().length();
i++;
if(Max < lengthStr){
Max= lengthStr;
viTriMax= i;
}
}
System.out.println("Do dai xau lon nhat la: "+Max+" o vi tri "+viTriMax);
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input= new Scanner(System.in);
System.out.println("Nhap vao 1 xau: ");
String strInput= input.nextLine();
timXauMax(strInput);
}
}
Kết quả chạy chương trình:
Nhap vao 1 xau:
Thich Qua Mon
Do dai xau lon nhat la: 5 o vi tri 1