/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem23;

/**
 * Given a file we are going to read it line by line, can you write a method to remove block
 * comments marked with '/*' and '*\'. Simulate the input file if needed.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class RemoveComments {
}

@@ -892,11 +892,1514 @@
 mments %7B
+%0A%0A  private static final char ASTERISK = '*';%0A  private static final char SLASH = '/';%0A  private static final char ANY_CHAR = 'c';%0A%0A  /**%0A   * Iterative solution to this problem. This algorithm has a complexity order in space terms%0A   * equals to O(1) because we are not using any additional data structure to store temporal data.%0A   * In time terms this algorithm has a complexity order equals to O(N) because we are going to%0A   * check every single character inside the file.%0A   *%0A   * This algorithm could be easier to read if we replace the char checks for indexOf methods. But%0A   * indexOf usage has a complexity order equals to O(N) and the result could be really slow.%0A   */%0A  public String remove(FakeFile file) %7B%0A    if (file == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null file as argument.%22);%0A    %7D%0A%0A    StringBuilder result = new StringBuilder();%0A    boolean openComment = false;%0A    String line = file.getLine();%0A    while (line != null) %7B%0A      char previous = ANY_CHAR;%0A%0A      for (char c : line.toCharArray()) %7B%0A        if (openComment) %7B%0A          if (c == SLASH && previous == ASTERISK) %7B%0A            openComment = false;%0A          %7D%0A        %7D else %7B%0A          if (c == ASTERISK && previous == SLASH) %7B%0A            openComment = true;%0A            result.deleteCharAt(result.length() - 1);%0A          %7D else %7B%0A            result.append(c);%0A          %7D%0A        %7D%0A        previous = c;%0A      %7D%0A      line = file.getLine();%0A    %7D%0A    return result.toString();%0A  %7D
 %0A%7D%0A

Add iterative implementation to problem 23