《算法竞赛入门(第二版)》--java实现.3.2字符数组.TeX中的引号

之前没有发现这是一本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));
    			}
    		}
    		
    	}
    	
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值