UVA – 272 – TEX Quotes

  • ID : UVA – 272 – TEX Quotes
  • Submissions :
    • Java – Accepted
  • Difficulty : Shame to Solve
  • Type : String Manipulation
  • Time for Submission : 15 minutes
  • Solution Description :
    • keep a boolean flag of the quotation opened status and replace each ‘”‘ with it’s equivalent.
  • Problems :
    • I remembered yesterday’s codeforces problem to replace the \ from the string “file\another” with a space. you have to use the regular expression “\\\\” with four \s because the \ in the regular expression has itself be skipped with another two. there are tricks concerning those regular expressions

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static boolean opened = false;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		ArrayList<String> sol = new ArrayList<String>();
		while (in.hasNext()) {
			sol.add(convert(in.nextLine()));
		}
		for (String s : sol)
			System.out.println(s);
	}

	private static String convert(String nextLine) {

		StringBuffer buf = new StringBuffer();
		for (char c : nextLine.toCharArray()) {
			if (c == '\"') {
				if (opened)
					buf.append("''");
				else
					buf.append("``");

				opened = !opened;
			}
			else
				buf.append(c);

		}
		return buf.toString();
	}
}
Advertisements

One thought on “UVA – 272 – TEX Quotes

  1. just use the ASCII code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: