@@ -0,0 +1,3711 @@
+package io.anand.learn;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.InputStreamReader;%0A%0Apublic class RemoveComments %7B%0A    // 1. Detect the comments start,%0A    // 2. Remember if we are in a comment.%0A    // 3. Look for comments end.%0A    // 4. During this state of comment section, do not output the input characters.%0A    // 5. Since comments can be in middle of the input string, output untill the start%0A    // 6. of the comment string.%0A    //%0A    // Single line comments are handled without any state if we are not already in%0A    // a multi-line comment.%0A%0A    static boolean commentStart = false;%0A    static boolean blockComment = false;   // /* */ style%0A    static String  singleChars  = %22//%22;%0A    static String  blockBeginChars = %22/*%22;%0A    static String  blockEndChars   = %22*/%22;%0A%0A    // Detect if we starting a new comment%0A    // Also determine if it is a block comment.%0A    // This method also outputs the characters till the start of the comment%0A    // to avoid looping again on the same string. Optionally can skip this if%0A    // only using to detect the string.%0A    static boolean detectCommentStart (String line, boolean outputEnabled) %7B%0A        // See if we have a single line comment%0A        int singleCommentIndex = line.indexOf(singleChars);%0A%0A        // See if we have a block comment start%0A        int blockCommentIndex = line.indexOf(blockBeginChars);%0A%0A        // Neither of them are present, just output the line and move on%0A        if ((-1 == singleCommentIndex) && (-1 == blockCommentIndex)) %7B%0A            System.out.println(line);%0A            return false;%0A        %7D%0A%0A        // Single line comment%0A        if (-1 != singleCommentIndex) %7B%0A            // if it precedes block comment start, ignore all after start of the single comment.%0A            if ((singleCommentIndex %3C blockCommentIndex) %7C%7C (-1 == blockCommentIndex))  %7B%0A                System.out.println(line.substring(0, singleCommentIndex));%0A                return false;%0A            %7D%0A        %7D%0A%0A        // Block comment present%0A        // We ignore single comment inside block comment.%0A        // If we started line with the comment, no substring,%0A        if (-1 != blockCommentIndex)%0A            System.out.println(line.substring(0, blockCommentIndex));%0A        commentStart = blockComment = true;%0A        // is there an end of comment in same line ?%0A        return detectCommentEnd(line.substring(blockCommentIndex + blockBeginChars.length()), outputEnabled);%0A    %7D%0A%0A    // Detect if we are ending a comment%0A    // End is only valid for a block comment%0A    // This method also outputs the characters till the start of the comment%0A    // to avoid loopoing again on the same string. Optionally can skip this if%0A    // only using to detect the string.%0A    static boolean detectCommentEnd (String line, boolean outputEnabled) %7B%0A        if (blockComment) %7B%0A            int commentIndex = line.indexOf(blockEndChars);%0A            // No end of the comment, So just skip...%0A            if (-1 == commentIndex) %7B%0A                return true;%0A            %7D%0A            // Found the end of the comment.%0A            commentStart = blockComment = false;%0A%0A            // Handle any single comment%0A            System.out.println(line.substring(commentIndex+blockEndChars.length()));%0A        %7D%0A        return false;%0A    %7D%0A%0A    public static void main (String%5B%5D args) throws java.lang.Exception%0A    %7B%0A        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));%0A        String input;%0A%0A        while (null != (input = br.readLine())) %7B%0A            if (false == commentStart) %7B%0A                detectCommentStart(input, true);%0A            %7D else %7B%0A                detectCommentEnd(input, true);%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Remove C comments