《算法竞赛入门经典》P45
在TeX中,左双引号是“``”,右双引号是“''”。输入一篇包含双引号的文章,你的任务是 把它转换成TeX的格式。
样例输入:
"To be or not to be," quoth the Bard, "that
is the question".
样例输出:
``To be or not to be,'' quoth the Bard, ``that
is the question''.
在Virtual Judge上提交了多次,终于AC了,感动~
Java和C语言不同,在网上查找了资料,说是不能在控制台一个一个读取单个字符,不能实现类似C语言中getchar()的功能。考虑了一下,决定采用scanner.nextLine()结合scanner.hasNextLine()一行一行的读取字符串,这样不会漏读空格,但是会漏读换行符,所以在读取循环中每读一行就输出一行,以实现样例输出。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String text="";
boolean isLeft=true;
while(in.hasNextLine())
{
text=in.nextLine();
for(int i=0;i<text.length();i++)
{
char c=text.charAt(i);
if(c=='"')
{
String punc=isLeft?"``":"''";
text=text.substring(0,i)+punc+text.substring(i+1);
isLeft=!isLeft;
}
}
System.out.println(text);
}
in.close();
}
}