之前没有发现这是一本C语言编写得书,但本人还未学习过C语言,只能尝试用java实现书中的代码,在此记录学习这本书的历程。
本人新手菜鸟,能力有限,如果有误或者有改进的地方欢迎指出,谢谢\(▔▽▔)/。
题目:在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' '.
//将输入样例以文件形式输入
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public static void main(String args[]) throws FileNotFoundException{
//rout为文件路径
String rout = "C:\\Users\\DELL\\Desktop\\testwork.txt"; //使用Scanner代替示例程序中的getchar()读取文件
Scanner input = new Scanner(new File(rout));
while(input.hasNextLine()) {
String token = input.nextLine();
int first = 0;
for(int i = 0;i < token.length();i++) {
if(token.charAt(i) == '"' ) { //不支持0代表false,其他为true,使用if语句代替示例中的printf
if(first == 0) {
System.out.print("``");
first++;
}else {
System.out.print("''");
first = 0;
}
}else {
System.out.print(token.charAt(i));
}
}
}
}