CPE UVA272-TeX Quotes[JAVA]

最後更新於 2021 年 8 月 24 日

題目原文https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=208

題目概要:將Input的雙引號用其他符號取代,第一個遇到的雙引號用“ 取代,第二個則用” 取代。

解題方向

非常容易的一題,只需要用一個變數count來判斷是第一個雙引號還是第二個雙引號。

可以使用 charAt() 方法來找雙引號。

Sample Input
“To be or not to be,” quoth the Bard, “that
is the question”.
The programming contestant replied: “I must disagree.
To C’ or not to C’, that is The Question!”

Sample Output
To be or not to be,” quoth the Bard,that
is the question”.
The programming contestant replied: I must disagree. To C’ or not to C’, that is The Question!”

程式碼

import java.util.*;
public class main{
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int count=1;
           	while(sc.hasNext()){
           		String str = sc.nextLine();
           		for(int i=0;i<str.length();i++){
           			if((str.charAt(i)=='"') && (count%2==1)){
           				System.out.print("``");
           				count++;
           			}else if((str.charAt(i)=='"') && (count%2==0)){
           				System.out.print("''");
           				count++;
           			}else{
           				System.out.print(str.charAt(i));
           			}
           		}
           		System.out.println();
           	}
    }
};
0 0 評分數
Article Rating
訂閱
通知
guest

0 Comments
在線反饋
查看所有評論