close

問題描述:

輸入一段英文文章 ( 包含英文字母、數字及符號 ) ,請依據分隔符號「 ” ,;.?” 」 ( 英文的空白、小寫逗號、分號、句號及問號 )  將文章切割成獨立的 Token, 並統計所指定的 Token 出現的次數 ( 大小寫不分 ) 。

例如:

分隔符號 = ” ,;.?”

統計的 Token = the

輸入一段英文文章 =

Write a program that reads in a line of text and then outputs that line of text with the first occurrence of hate changed to love. For example, the user Enter a line of text as follow.

結果 : 2

輸入說明:

Input file

第 1 行 : 分隔符號用 ”” 括起來

第 2 行 : 指定要統計的 Token

第 3 行 : 所給定的文章

” ,;.?”

The

Write a program that reads in a line of text and then outputs that line of text with the first occurrence of hate changed to love. For example, the user Enter a line of text as follow.

範例:

輸出輸入測試資料

題目來源:http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7012

import java.util.Scanner;

public class C_ST50 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String x=scanner.nextLine();
        String token=scanner.nextLine().toLowerCase();
        String[] str=scanner.nextLine().toLowerCase().split(" |\\,|\\;|\\.|\\?");
        int num=0;
        for(int i=0;i<str.length;i++){
            if(str[i].equals(token)){
                num++;
            }
        }
        System.out.println(num);
        scanner.close();
    }}

 

arrow
arrow
    全站熱搜

    阿雅 發表在 痞客邦 留言(0) 人氣()